Skip to content

Latest commit

 

History

History
64 lines (53 loc) · 2.56 KB

README.md

File metadata and controls

64 lines (53 loc) · 2.56 KB

Serilog enricher for class name

Build NuGet NuGet Version NuGet Downloads

Enrich Serilog logs with class name only.

Quick Start

Use .Enrich.WithClassName() extension

internal static IServiceCollection AddLogging(this IServiceCollection services, IConfiguration configuration)
{
    return services.AddLogging(builder => builder
        .AddSerilog(new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .Enrich.WithClassName()
            .CreateLogger(), dispose: true));
}

or add "Enrich": [ "WithClassName" ] to appsettings.json file

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Enrichers.Environment" ],
    "MinimumLevel": "Information",
    "Enrich": [ "WithMachineName", "WithClassName" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] [{MachineName}] [{ClassName}] {Message}{NewLine}{Exception}"
        }
      }
    ]
  }
}

Note: Since serilog can automatically find the enricher adding "Using": [ "Serilog.Enrichers.ClassName" ] is not required. You must add if you use use -p:PublishSingleFile=true.

Publish you application as single file

When publishing application with -p:PublishSingleFile=true you must include "Using": [ "Serilog.Enrichers.ClassName" ] in appsettings.json. Without this the enricher will not be loaded and the value will be empty. For example:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Environment", "Serilog.Enrichers.ClassName" ],
    ...
  }
}

Created from JandaBox

Inspired by Serilog.Enrichers.ShortTypeName