PragmaticFlow / Serilog.Sinks.SpectreConsole

Apache License 2.0
32 stars 7 forks source link

Serilog.Sinks.SpectreConsole

build NuGet Gitter

A Serilog sink that writes log events to console using Spectre.Console. \ Output is plain text. \ The sink is written in F#.

Getting started

The sink is available as a NuGet package. \ You can install it using the following command:

Install-Package Serilog.Sinks.SpectreConsole

To enable the sink, use .SpectreConsole() extension method.

Log.Logger = new LoggerConfiguration()
    .WriteTo.SpectreConsole("{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", minLevel: LogEventLevel.Information)
    .MinimumLevel.Verbose()
    .CreateLogger();

Log.Information("Information level example with {0}", "parameter");

For more information, take a look at examples.

Configuration via appsettings.json

To configure the sink via 'appsettings.json' configuration, you have to install NuGet packages:

Install-Package Microsoft.Extensions.Configuration.Json Install-Package Serilog.Settings.Configuration

Then use ReadFrom.Configuration() method.

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

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

In appsettings.json configuration file, write the following section:

"Serilog": {
    "WriteTo": [
      {
        "Name": "SpectreConsole",
        "Args": {
          "outputTemplate": "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
          "minLevel": "Verbose"
        }
      }
    ]
}