Serilog.Enrichers.AspNetCore.HttpContext 1.0.0

Serilog Enricher to Set Properties From AspNetCore HttpContext to Serilog LogContext

There is a newer version of this package available.
See the version list below for details.
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.
#r "nuget: Serilog.Enrichers.AspNetCore.HttpContext, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Enrichers.AspNetCore.HttpContext as a Cake Addin
#addin nuget:?package=Serilog.Enrichers.AspNetCore.HttpContext&version=1.0.0

// Install Serilog.Enrichers.AspNetCore.HttpContext as a Cake Tool
#tool nuget:?package=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="Demo", RequestId="0HLFDGEJTQ5RQ:00000001", RequestPath="/", CorrelationId=null, ConnectionId="0HLFDGEJTQ5RQ"}

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="Demo", RequestId="0HLFDGEJTQ5RQ:00000001", RequestPath="/", CorrelationId=null, ConnectionId="0HLFDGEJTQ5RQ"}

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Serilog.Enrichers.AspNetCore.HttpContext:

Package Downloads
TraceIdentifiers.AspNetCore.Serilog
AspNetCore middleware to push TraceIdentifiers to Serilog LogContext
Cloudburst.Logging
Cloudburst Microservice Toolkit: Logging for microservices using Serilog and Cloudburst.Configuration
My.CorrelationId.AspNetCore.Serilog
Enrich Correlation Id as well get correlation id where needed.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 234,195 11/12/2019
1.0.0 104,461 7/19/2018