### Install HttpUserAgentParser.AspNetCore Package Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Add the MyCSharp.HttpUserAgentParser.AspNetCore NuGet package to your project using the .NET CLI. ```bash dotnet add package MyCSharp.HttpUserAgentParser.AspNetCore ``` -------------------------------- ### Install HttpUserAgentParser Package Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Use the dotnet CLI to add the MyCSharp.HttpUserAgentParser package to your project. ```bash dotnet add package MyCSharp.HttpUserAgentParser ``` -------------------------------- ### Quick Start: Parse User Agent String Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Demonstrates how to parse a raw HTTP User-Agent string into a structured object without using dependency injection. ```csharp string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"; HttpUserAgentInformation info = HttpUserAgentParser.Parse(userAgent); // or: HttpUserAgentInformation.Parse(userAgent) ``` -------------------------------- ### Enable Core Parser Telemetry (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Use this snippet to enable core parser telemetry via the fluent API. This is the basic setup for telemetry. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentParser() .WithTelemetry(); } ``` -------------------------------- ### Enable Core Parser Meters (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Use this snippet to enable core parser meters using System.Diagnostics.Metrics via the fluent API. This is the basic setup for meters. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentParser() .WithMeterTelemetry(); } ``` -------------------------------- ### Configure OpenTelemetry Metrics for EventCounters Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Set up OpenTelemetry to collect EventCounters from HttpUserAgentParser. Ensure necessary OpenTelemetry packages and an exporter are installed. ```csharp using OpenTelemetry.Metrics; builder.Services.AddOpenTelemetry() .WithMetrics(metrics => { metrics .AddEventCountersInstrumentation(options => { options.AddEventSources( HttpUserAgentParserEventSource.EventSourceName, HttpUserAgentParserMemoryCacheEventSource.EventSourceName, HttpUserAgentParserAspNetCoreEventSource.EventSourceName); }) .AddOtlpExporter(); }); ``` -------------------------------- ### Get Raw User-Agent String Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Retrieve the raw User-Agent string directly from the current HttpContext without parsing. ```csharp string? ua = HttpContext.GetUserAgentString(); ``` -------------------------------- ### Add HttpUserAgentParser MemoryCache Package Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Installs the memory cache package for HttpUserAgentParser. ```bash dotnet add package MyCSharp.HttpUserAgentParser.MemoryCache ``` -------------------------------- ### Get User Agent Information in ASP.NET Core Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Retrieves HttpUserAgentInformation using the injected IHttpUserAgentParserAccessor within an ASP.NET Core application. This pattern simplifies accessing user agent data. ```csharp public void MyMethod(IHttpUserAgentParserAccessor parserAccessor) { HttpUserAgentInformation info = parserAccessor.Get(); } ``` -------------------------------- ### Enable Native Meters via Fluent API Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Enable opt-in telemetry using native System.Diagnostics.Metrics instruments through the library's fluent API configuration. ```csharp services .AddHttpUserAgentParser() .WithMeterTelemetry(); ``` -------------------------------- ### Enable ASP.NET Core Meters (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Enable ASP.NET Core specific meters, which track user agent header presence or absence via System.Diagnostics.Metrics. Requires the parser accessor. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentMemoryCachedParser() .AddHttpUserAgentParserAccessor() .WithAspNetCoreMeterTelemetry(); } ``` -------------------------------- ### Inject IHttpUserAgentParserProvider Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Demonstrates how to inject and use the IHttpUserAgentParserProvider after it has been registered. This allows parsing user agent strings. ```csharp public sealed class MyService(IHttpUserAgentParserProvider parser) { public HttpUserAgentInformation Parse(string userAgent) => parser.Parse(userAgent); } ``` -------------------------------- ### Parse User Agent String Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Parses a given user agent string into detailed information. This is the basic usage of the library. ```csharp string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"; HttpUserAgentInformation info = HttpUserAgentParser.Parse(userAgent); // alias HttpUserAgentInformation.Parse() ``` ```csharp UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" Type = HttpUserAgentType.Browser Platform = { Name = "Windows 10", PlatformType = HttpUserAgentPlatformType.Windows } Name = "Chrome" Version = "90.0.4430.212" MobileDeviceType = null ``` -------------------------------- ### Benchmark Results for HttpUserAgentParser Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Performance comparison of HttpUserAgentParser against other libraries across different scenarios (uncached/cached, user agents). ```shell BenchmarkDotNet v0.15.8, Windows 10 (10.0.19045.6691/22H2/2022Update) AMD Ryzen 9 9950X 4.30GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 10.0.101 [Host] : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v4 ShortRun : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v4 Job=ShortRun IterationCount=3 LaunchCount=1 WarmupCount=3 | Method | Categories | Data | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | |------------------- |----------- |------------- |----------------:|-----------------:|---------------:|----------:|--------:|---------:|---------:|---------:|-----------:|------------:| | MyCSharp | Basic | Chrome Win10 | 939.54 ns | 113.807 ns | 6.238 ns | 1.00 | 0.01 | 0.0019 | - | - | 48 B | 1.00 | | UAParser | Basic | Chrome Win10 | 9,120,055.21 ns | 2,108,412.449 ns | 115,569.201 ns | 9,707.23 | 120.28 | 671.8750 | 609.3750 | 109.3750 | 11659008 B | 242,896.00 | | DeviceDetector.NET | Basic | Chrome Win10 | 5,099,680.21 ns | 5,313,448.322 ns | 291,248.033 ns | 5,428.01 | 270.28 | 296.8750 | 140.6250 | 31.2500 | 5034130 B | 104,877.71 | | | | | | | | | | | | | | | | MyCSharp | Basic | Google-Bot | 226.47 ns | 20.818 ns | 1.141 ns | 1.00 | 0.01 | - | - | - | - | NA | | UAParser | Basic | Google-Bot | 9,007,285.42 ns | 491,694.016 ns | 26,951.408 ns | 39,772.36 | 202.28 | 687.5000 | 640.6250 | 125.0000 | 12015474 B | NA | | DeviceDetector.NET | Basic | Google-Bot | 6,056,996.61 ns | 567,479.924 ns | 31,105.490 ns | 26,745.13 | 166.88 | 546.8750 | 132.8125 | 23.4375 | 8862491 B | NA | | | | | | | | | | | | | | | | MyCSharp | Cached | Chrome Win10 | 24.59 ns | 2.222 ns | 0.122 ns | 1.00 | 0.01 | - | - | - | - | NA | | UAParser | Cached | Chrome Win10 | 162,917.93 ns | 36,544.250 ns | 2,003.114 ns | 6,625.90 | 76.03 | 2.1973 | - | - | 37488 B | NA | | | | | | | | | | | | | | | | MyCSharp | Cached | Google-Bot | 17.42 ns | 1.077 ns | 0.059 ns | 1.00 | 0.00 | - | - | - | - | NA | | UAParser | Cached | Google-Bot | 126,321.45 ns | 3,171.908 ns | 173.863 ns | 7,253.51 | 23.01 | 2.6855 | - | - | 45856 B | NA | ``` -------------------------------- ### Enable Telemetry (EventCounters) via Fluent API Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Enable opt-in telemetry using EventCounters through the library's fluent API configuration. ```csharp services .AddHttpUserAgentParser() .WithTelemetry(); ``` -------------------------------- ### Parse User Agent with Dependency Injection Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Demonstrates parsing a user agent string using an injected IHttpUserAgentParserProvider. This is useful for integrating with DI containers. ```csharp private IHttpUserAgentParserProvider _parser; public void MyMethod(string userAgent) { HttpUserAgentInformation info = _parser.Parse(userAgent); } ``` -------------------------------- ### Enable ASP.NET Core Telemetry (Native Meters) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Enable telemetry using native System.Diagnostics.Metrics instruments for the HttpUserAgentParser accessor via the fluent API. ```csharp services .AddHttpUserAgentParserAccessor() .WithAspNetCoreMeterTelemetry(); ``` -------------------------------- ### Enable MemoryCache Meters (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Enable both core parser meters and optional memory cache meters. This provides more detailed cache performance metrics via System.Diagnostics.Metrics. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentMemoryCachedParser() .WithMeterTelemetry() // core meters (optional) .WithMemoryCacheMeterTelemetry(); } ``` -------------------------------- ### Configure Meter Telemetry Prefix (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Configure the default prefix for meter names. The prefix can be empty to remove it, or must be alphanumeric and end with a dot. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentParser() .WithMeterTelemetryPrefix("acme."); } ``` -------------------------------- ### Enable Memory Cache Meter Telemetry (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Enables native System.Diagnostics.Metrics telemetry for the memory cache provider using a fluent API. This allows for more modern metric collection. ```csharp services .AddHttpUserAgentMemoryCachedParser() .WithMemoryCacheMeterTelemetry(); ``` -------------------------------- ### Enable Memory Cache Telemetry (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Enables telemetry for the memory cache provider using a fluent API. This allows monitoring of cache hits and misses. ```csharp services .AddHttpUserAgentMemoryCachedParser() .WithMemoryCacheTelemetry(); ``` -------------------------------- ### Monitor EventCounters with dotnet-counters Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Use the dotnet-counters CLI tool to monitor the EventCounters emitted by the HttpUserAgentParser.AspNetCore package. ```bash dotnet-counters monitor --process-id MyCSharp.HttpUserAgentParser.AspNetCore ``` -------------------------------- ### Configure Memory Cache Options Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Configures specific options for the memory cache, such as sliding expiration and size limits. This allows customization of cache behavior. ```csharp services.AddHttpUserAgentMemoryCachedParser(options => { options.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(60); // default is 1 day options.CacheOptions.SizeLimit = 1024; // default is null (= no limit) }); ``` -------------------------------- ### Export Native Meters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Configure OpenTelemetry Metrics to collect native meters emitted by the HttpUserAgentParser.AspNetCore package. ```csharp using OpenTelemetry.Metrics; using MyCSharp.HttpUserAgentParser.AspNetCore.Telemetry; metrics.AddMeter(HttpUserAgentParserAspNetCoreMeters.MeterName); ``` -------------------------------- ### Dependency Injection: Add HttpUserAgentParser Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Configure services to use the HttpUserAgentParser with dependency injection, opting for no caching. ```csharp services .AddHttpUserAgentParser(); ``` -------------------------------- ### Enable Core and Memory Cache Meter Telemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Enables both core and memory cache native meters. This provides comprehensive metric collection for the user agent parsing process. ```csharp services .AddHttpUserAgentMemoryCachedParser() .WithMeterTelemetry() .WithMemoryCacheMeterTelemetry(); ``` -------------------------------- ### Export Native Meters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Configure OpenTelemetry to collect native meters emitted by MyCSharp.HttpUserAgentParser and export them using an OTLP exporter. ```csharp using OpenTelemetry.Metrics; using MyCSharp.HttpUserAgentParser.Telemetry; builder.Services.AddOpenTelemetry() .WithMetrics(metrics => { metrics .AddMeter(HttpUserAgentParserMeters.MeterName) .AddOtlpExporter(); }); ``` -------------------------------- ### Enable ASP.NET Core Telemetry (EventCounters) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Enable telemetry using EventCounters for the HttpUserAgentParser accessor via the fluent API. ```csharp services .AddHttpUserAgentParserAccessor() .WithAspNetCoreTelemetry(); ``` -------------------------------- ### Enable Core and Memory Cache Telemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Enables both core and memory cache telemetry. This provides comprehensive monitoring of the user agent parsing process. ```csharp services .AddHttpUserAgentMemoryCachedParser() .WithTelemetry() .WithMemoryCacheTelemetry(); ``` -------------------------------- ### Enable ASP.NET Core Telemetry (Fluent API) Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Enable ASP.NET Core specific telemetry, which tracks user agent header presence or absence. Requires the parser accessor. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentMemoryCachedParser() .AddHttpUserAgentParserAccessor() .WithAspNetCoreTelemetry(); } ``` -------------------------------- ### Monitor EventCounters with dotnet-counters Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Use the dotnet-counters CLI tool to monitor the EventCounters emitted by the HttpUserAgentParser for a specific process. ```bash dotnet-counters monitor --process-id MyCSharp.HttpUserAgentParser ``` -------------------------------- ### Add Memory Cache Provider Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Registers the IMemoryCache-based caching provider for HttpUserAgentParser. This should be called during application startup. ```csharp services.AddHttpUserAgentMemoryCachedParser(); ``` -------------------------------- ### Configure Memory Cache Options Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Registers the HttpUserAgentParserMemoryCachedProvider and allows customization of cache entry options, such as sliding expiration and size limits. This is recommended for web applications. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddHttpUserAgentMemoryCachedParser(); // or use options services.AddHttpUserAgentMemoryCachedParser(options => { options.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(60); // default is 1 day // limit the total entries in the MemoryCache // each unique user agent string counts as one entry options.CacheOptions.SizeLimit = 1024; // default is null (= no limit) }); } ``` -------------------------------- ### Monitor with dotnet-counters Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Command to monitor EventCounters for the MyCSharp.HttpUserAgentParser.MemoryCache process. Replace with the actual process ID. ```bash dotnet-counters monitor --process-id MyCSharp.HttpUserAgentParser.MemoryCache ``` -------------------------------- ### Export Native Meters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Configures OpenTelemetry metrics instrumentation to collect native meters from the memory cache provider. This is used when native meter telemetry is enabled. ```csharp using OpenTelemetry.Metrics; using MyCSharp.HttpUserAgentParser.MemoryCache.Telemetry; metrics.AddMeter(HttpUserAgentParserMemoryCacheMeters.MeterName); ``` -------------------------------- ### Export EventCounters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Configure OpenTelemetry to collect EventCounters from the parser, memory cache, and ASP.NET Core integrations, and export them via OTLP. ```csharp using OpenTelemetry.Metrics; using MyCSharp.HttpUserAgentParser.Telemetry; using MyCSharp.HttpUserAgentParser.MemoryCache.Telemetry; using MyCSharp.HttpUserAgentParser.AspNetCore.Telemetry; builder.Services.AddOpenTelemetry() .WithMetrics(metrics => { metrics .AddEventCountersInstrumentation(options => { options.AddEventSources( HttpUserAgentParserEventSource.EventSourceName, HttpUserAgentParserMemoryCacheEventSource.EventSourceName, HttpUserAgentParserAspNetCoreEventSource.EventSourceName); }) .AddOtlpExporter(); }); ``` -------------------------------- ### Export EventCounters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Configure OpenTelemetry to collect EventCounters from MyCSharp.HttpUserAgentParser and export them using an OTLP exporter. ```csharp using OpenTelemetry.Metrics; using MyCSharp.HttpUserAgentParser.Telemetry; builder.Services.AddOpenTelemetry() .WithMetrics(metrics => { metrics .AddEventCountersInstrumentation(options => { options.AddEventSources(HttpUserAgentParserEventSource.EventSourceName); }) .AddOtlpExporter(); }); ``` -------------------------------- ### Register Parser and Accessor in ASP.NET Core Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Register an HTTP User-Agent parser provider and the accessor service in your ASP.NET Core application's dependency injection container. ```csharp services .AddHttpUserAgentMemoryCachedParser() // or: AddHttpUserAgentParser / AddHttpUserAgentCachedParser .AddHttpUserAgentParserAccessor(); ``` -------------------------------- ### Register Cached Parser Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Configures service collection to use a cached parser implementation, such as HttpUserAgentParserCachedProvider. This is recommended for performance improvements. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddHttpUserAgentCachedParser(); // uses `HttpUserAgentParserCachedProvider` // or // services.AddHttpUserAgentParser(); } ``` -------------------------------- ### Register Default Parser without Caching Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Configures service collection to use the default HttpUserAgentParserProvider, which does not perform any caching. Suitable for scenarios where caching is not needed. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddHttpUserAgentParser(); // uses HttpUserAgentParserDefaultProvider and does not cache } ``` -------------------------------- ### Export EventCounters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Configure OpenTelemetry Metrics to collect EventCounters from the HttpUserAgentParser.AspNetCore EventSource. ```csharp using OpenTelemetry.Metrics; metrics.AddEventCountersInstrumentation(options => { options.AddEventSources(HttpUserAgentParserAspNetCoreEventSource.EventSourceName); }); ``` -------------------------------- ### Export EventCounters to OpenTelemetry Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.MemoryCache/readme.md Configures OpenTelemetry metrics instrumentation to add EventCounters from the memory cache provider. This allows exporting metrics to various backends. ```csharp using OpenTelemetry.Metrics; metrics.AddEventCountersInstrumentation(options => { options.AddEventSources(HttpUserAgentParserMemoryCacheEventSource.EventSourceName); }); ``` -------------------------------- ### Access User-Agent Information in Controller Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser.AspNetCore/readme.md Inject and use the IHttpUserAgentParserAccessor to retrieve parsed User-Agent information within an ASP.NET Core controller. ```csharp public sealed class MyController(IHttpUserAgentParserAccessor accessor) { public HttpUserAgentInformation Get() => accessor.Get(); } ``` -------------------------------- ### Dependency Injection: Add Cached HttpUserAgentParser Source: https://github.com/mycsharp/httpuseragentparser/blob/main/src/HttpUserAgentParser/readme.md Configure services to use a cached version of the HttpUserAgentParser for improved performance, utilizing ConcurrentDictionary. ```csharp services .AddHttpUserAgentCachedParser(); // or: .AddHttpUserAgentParser(); ``` -------------------------------- ### Register ASP.NET Core Parser Accessor Source: https://github.com/mycsharp/httpuseragentparser/blob/main/README.md Configures service collection to add an IHttpUserAgentParserAccessor for ASP.NET Core applications. This accessor retrieves the user agent from the HttpContext. ```csharp public void ConfigureServices(IServiceCollection services) { services .AddHttpUserAgentMemoryCachedParser() // registers Parser, returns HttpUserAgentParserDependencyInjectionOptions // or use any other Parser registration like services.AddHttpUserAgentParser(); above .AddHttpUserAgentParserAccessor(); // registers IHttpUserAgentParserAccessor, uses IHttpUserAgentParserProvider } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.