- .Net Core Serilog – Basic
- .Net Core Serilog – Configuration
- .Net Core Serilog - Structured JSON output
- .Net Core Serilog - Enrichers
- .Net Core Serilog - Custom JSON output
- .Net Core Serilog - Adding Sinks
This is part 5 of 6.
Serilog’s “CompactJsonFormatter” may not be what you want. In cases where you need to more control on field names and data, you can use the “Expression Template”.
-
In Program.cs, add “using Serilog.Templates” to the top of the file and change the CreateHostBuilder() method to the following:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.Enrich.WithProperty("Assembly", typeof(Program).Assembly.GetName().Name)
.WriteTo.Console(new ExpressionTemplate("{ {app_timestamp:@t, message:@m, rendering:@r, level:if @l = 'Debug' then 'DEBUG' else if @l = 'Information' then 'INFO' else if @l = 'Warning' then 'WARN' else if @l = 'Error' then 'ERR' else if @l = 'Fatal' then 'FTL' else @l, exception:@x, ..@p} }\n"))
);