Serilog.Enrichers.AspNetCore.HttpContext 1.0.0

Serilog Enricher to Set Properties From AspNetCore HttpContext to Serilog LogContext

Install-Package Serilog.Enrichers.AspNetCore.HttpContext -Version 1.0.0
dotnet add package Serilog.Enrichers.AspNetCore.HttpContext --version 1.0.0
<PackageReference Include="Serilog.Enrichers.AspNetCore.HttpContext" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Enrichers.AspNetCore.HttpContext --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Serilog Enrichers AspNetCore HttpContext

AspNetCore middleware to add to Serilog LogContext properties from HttpContext

Usage

Register this middleware as early as possible to use HttpContext properties in chained middlewares.

Code Sample

        public void ConfigureServices(IServiceCollection services)
        {
            LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
            loggerConfiguration.Enrich.FromLogContext(); // mandatory
            loggerConfiguration.WriteTo.Console(
                outputTemplate:
                "[{Timestamp:HH:mm:ss} {Level:u3}] {EventId} {Message:lj} {Properties}{NewLine}{Exception}{NewLine}");

            Log.Logger = loggerConfiguration.CreateLogger();

            services.AddLogging(builder => builder.ClearProviders().AddSerilog(dispose: true));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSerilogLogContext(options =>
            {
                options.EnrichersForContextFactory = context => new[]
                {
                    // TraceIdentifier property will be available in all chained middlewares. And yes - it is HttpContext specific
                    new PropertyEnricher("TraceIdentifier", context.TraceIdentifier) 
                };
            });

            app.Run(async (context) =>
            {
                // No need to add TraceIdentifier as a parameter to "LogInformation" method
                context.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("Demo").LogInformation("Hellow World Log. TraceId: {TraceIdentifier}");
                await context.Response.WriteAsync("Hello World!");
            });
        }

Console Optput

[17:26:46 INF] Hellow World Log. TraceId: 0HLFDGEJTQ5RQ:00000001 {SourceContext=&quot;Demo&quot;, RequestId=&quot;0HLFDGEJTQ5RQ:00000001&quot;, RequestPath=&quot;/&quot;, CorrelationId=null, ConnectionId=&quot;0HLFDGEJTQ5RQ&quot;}

Serilog Enrichers AspNetCore HttpContext

AspNetCore middleware to add to Serilog LogContext properties from HttpContext

Usage

Register this middleware as early as possible to use HttpContext properties in chained middlewares.

Code Sample

        public void ConfigureServices(IServiceCollection services)
        {
            LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
            loggerConfiguration.Enrich.FromLogContext(); // mandatory
            loggerConfiguration.WriteTo.Console(
                outputTemplate:
                "[{Timestamp:HH:mm:ss} {Level:u3}] {EventId} {Message:lj} {Properties}{NewLine}{Exception}{NewLine}");

            Log.Logger = loggerConfiguration.CreateLogger();

            services.AddLogging(builder => builder.ClearProviders().AddSerilog(dispose: true));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSerilogLogContext(options =>
            {
                options.EnrichersForContextFactory = context => new[]
                {
                    // TraceIdentifier property will be available in all chained middlewares. And yes - it is HttpContext specific
                    new PropertyEnricher("TraceIdentifier", context.TraceIdentifier) 
                };
            });

            app.Run(async (context) =>
            {
                // No need to add TraceIdentifier as a parameter to "LogInformation" method
                context.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("Demo").LogInformation("Hellow World Log. TraceId: {TraceIdentifier}");
                await context.Response.WriteAsync("Hello World!");
            });
        }

Console Optput

[17:26:46 INF] Hellow World Log. TraceId: 0HLFDGEJTQ5RQ:00000001 {SourceContext=&quot;Demo&quot;, RequestId=&quot;0HLFDGEJTQ5RQ:00000001&quot;, RequestPath=&quot;/&quot;, CorrelationId=null, ConnectionId=&quot;0HLFDGEJTQ5RQ&quot;}

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 40,454 7/19/2018