DeviceDetector.NET

repository·master·Indexed 19 days ago

https://github.com/totpero/devicedetector.net

A C# library for .NET that parses User Agent strings and Client Hints to detect devices (desktop, tablet, mobile, etc.), clients (browsers, media players, etc.), operating systems, brands, and models. A port of the PHP device-detector library, it includes specialized parsers for bots and crawlers, as well as optional icon path resolution packages for Matomo and Simbiat icon packs.

Tokens
34.9K
Snippets
65
Records
102
Agent score
64%

What's inside DeviceDetector.NET

  1. Overview of DeviceDetector.NET capabilities

    master

    DeviceDetector.NET is a universal device detection library for .NET that parses HTTP User-Agent strings (and optionally User-Agent Client Hints) to identify various entities. It is a faithful port of the PHP matomo-org/device-detector library, meaning it uses the same regex rule set.

    It can detect:

    • Devices: desktop, smartphone, tablet, phablet, TV, console, car browser, camera, portable media player, wearable, smart display, smart speaker, peripheral.
    • Clients: browsers, feed readers, media players, mobile apps, PIMs, libraries.
    • Operating systems: name, version, and family.
    • Bots: crawlers, spiders, and monitoring tools.
    • Brands and models: specific hardware/mobile device identifiers.
  2. Use DeviceDetector.NET.Icons.Common for icon-path-resolution

    master

    The DeviceDetector.NET.Icons.Common package provides shared icon-path-resolution primitives.

    Important: This package is not intended to be referenced directly by application code. Instead, you should install one of the following packages depending on your requirements:

    • DeviceDetector.NET.Icons (Simbiat's icon pack)
    • DeviceDetector.NET.Icons.Matomo (Official Matomo icon pack)
  3. What DeviceDetector.NET is able to detect

    master

    DeviceDetector.NET provides detection capabilities for a wide range of operating systems and browsers. The detection lists are auto-generated and updated periodically.

    Note: The lists provided in the documentation are subject to change and may not always be exhaustive. As of the last update on 2026/07/30, the library supports a vast array of platforms including major desktop OSs (Windows, Mac, Linux), mobile OSs (Android, iOS, HarmonyOS), and specialized embedded or gaming systems.

  4. What DeviceDetector.NET can detect

    master

    DeviceDetector.NET is capable of identifying a wide range of client information, including:

    • Mobile Apps: A vast list of applications, including those built with AFNetworking or Electron.
    • PIMs (Personal Information Managers): Email and productivity clients like Gmail, Microsoft Outlook, and Thunderbird.
    • Feed Readers: RSS and podcast clients such as Reeder, NewsBlur, and NetNewsWire.
    • Media Players: Various desktop and mobile audio/video players.
    • Operating Systems and Browsers: (Refer to the full documentation for complete lists).

    Note: The detection capabilities are extensive and cover various categories of software and hardware identifiers.

  5. Per-request caching behavior in the enricher

    master

    To optimize performance, the User-Agent is parsed at most once per request. The first time a log event is enriched during a request, the result is stored in HttpContext.Items. Subsequent log calls within the same request reuse this cached property.

    Note: This process forces VersionTruncation.VERSION_TRUNCATION_NONE, meaning logs will always contain full version numbers regardless of your global DeviceDetector.SetVersionTruncation settings.

  6. Compare SetCache and LRUCachedDeviceDetector

    master

    Choosing between SetCache and LRUCachedDeviceDetector depends on whether you need Client Hint support and how much of the parsing lifecycle you want to skip.

    Featuredd.SetCache(new DictionaryCache())
    What it cachesInternal regex-matching fragments inside one parse
    ScopeProcess-wide static dictionary; you still call new DeviceDetector() and .Parse()
    Client HintsFull support — pass them to the constructor
    Best Use CaseAlways safe to use; improves performance of every Parse() call
    FeatureLRUCachedDeviceDetector.GetDeviceDetector(ua)
    What it cachesEntire, already-parsed DeviceDetector instances, keyed by UA
    ScopeReturns a shared instance; you never call new or .Parse() yourself
    Client HintsNot supported — only takes a userAgent string
    Best Use CaseWhen you only have a UA string and want to skip re-parsing entirely

    If you need both Client Hints and whole-instance caching, you must implement a custom wrapper that includes Client Hint values in your own cache key.

  7. Optimize Device Detection performance with Caching

    master

    Device detection performance can be managed at two levels:

    1. Fragment Caching (Default): Creating a new DeviceDetector instance per request is lightweight and efficient. The library automatically uses a process-wide fragment cache for regex results. No extra configuration is required for this to work.
    2. Instance Caching (High Load): If parsing becomes a bottleneck under heavy load, you can use LRUCachedDeviceDetector.GetDeviceDetector(userAgent).

    Warning: Using the LRU cache approach does not support Client Hints. Only use this if you do not require Client Hint data for your specific detection needs.

  8. Use DeviceDetector.NET outside of ASP.NET Core

    master
    The core DeviceDetector.NET package is platform-agnostic and has no dependency on ASP.NET Core. It only requires a User-Agent string (and optionally a headers dictionary for Client Hints) to function. ASP.NET Core-specific features like the Serilog enricher or the demo API are provided in separate, optional packages.
  9. Choose between Serilog enrichment and Application Logic detection

    master

    Decide how to integrate device detection based on your use case:

    • For Logging: Use the Serilog.Enrichers.AspNetCore.DeviceDetector package if you want device information (like OS or Browser) to be automatically attached to every log entry (errors, warnings, etc.) for a request.
    • For Application Logic: Use DeviceDetector.GetInfoFromUserAgent or the Dependency Injection wrapper pattern if you need to change application behavior based on the device (e.g., serving different HTML/CSS for mobile users).
  10. Choose and configure a Regex Engine

    master

    DeviceDetector.NET abstracts regex matching behind the IRegexEngine interface. By default, the library uses MSRegexCompiledEngine, which is backed by .NET's System.Text.RegularExpressions.

    You can swap the engine using DeviceDetector.SetRegexEngine(IRegexEngine) or by calling SetRegexEngine on individual parsers (AbstractParser). If no engine is explicitly set, GetRegexEngine() will lazily default to MSRegexCompiledEngine.

    // Example of setting a specific engine
    var dd = new DeviceDetector(userAgent, clientHints);
    dd.SetRegexEngine(new PcreRegexEngine());
    dd.Parse();
  11. What DeviceDetector.NET is able to detect

    master

    DeviceDetector.NET is a comprehensive library capable of identifying a wide range of hardware and software attributes from client identifiers (like User-Agent strings). It can detect:

    • Operating Systems: Various desktop, mobile, and embedded OS versions.
    • Browsers: A vast list of web browsers and their specific versions.
    • Device Brands: Hundreds of manufacturers (e.g., Apple, Samsung, Xiaomi, Sony, etc.).
    • Device Models: Specific hardware models associated with brands.
    • Device Types: Classification of devices (e.g., mobile, tablet, desktop, smart TV, console).
    • Other metadata: Additional device-related information provided by the detection engine.