vip32 / serilog-enrichers-shorttypename

Enrich Serilog logs with just the type name, excluding namespaces.

Home Page:https://www.nuget.org/packages/Serilog.Enrichers.ShortTypeName/

Repository from Github https://github.comvip32/serilog-enrichers-shorttypenameRepository from Github https://github.comvip32/serilog-enrichers-shorttypename

Serilog.Enrichers.ShortTypeName

Enriches logs with just the class name. This requires the log to already contain the SourceContext property.

Logs with SourceContext "MyAssembly.MyNamespace.MyType", will be enriched with ShortTypeName "MyType".

Usage

Best used with Serilog.Extensions.Logging which will automatically enrich the logs with SourceContext that's required for this enricher.

Set the output template to something like "[{Timestamp:HH:mm:ss} {Level:u3}] ({TypeName}) {Message:lj}{NewLine}{Exception}"

Example

appsettings.json

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Enrichers.ShortTypeName" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Warning",
        "Microsoft": "Warning",
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] ({ShortTypeName}) {Message:lj}{NewLine}{Exception}"
        }
      },
    ],
    "Enrich": [ "WithShortTypeName" ]
  }
}

Startup code

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .Build();

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

var services = new ServiceCollection();
services.AddLogging(loggingBuilder =>
    loggingBuilder.AddSerilog(dispose: true));

Usage

public class MyClass
{
    private readonly ILogger<MyClass> logger;

    public MyClass(ILogger<MyClass> logger)
    {
        this.logger = logger;
    }

    public void DoSomething()
    {
        this.logger.LogInformation("hello world");
    }
}

Outputs: [21:14:35 INF] (MyClass) hello world

About

Enrich Serilog logs with just the type name, excluding namespaces.

https://www.nuget.org/packages/Serilog.Enrichers.ShortTypeName/

License:MIT License


Languages

Language:C# 100.0%