Skip to content

Jandini/serilog-enrichers-classname

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Enrich Serilog logs with class name only

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages