I’m a big fan of structured logging in general and Serilog in particular. Especially in combination with Seq it is unbeatable.
Today I lost some time searching how to specify the Seq log sink using configuration. Here are the steps I had to take:
- Add the Serilog.Settings.Configuration NuGet package to your project(this one is for ASP.NET Core, others exists for web.config, …)
- Create a LoggerConfiguration:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
ConfigureLogging(); | |
} | |
public IConfiguration Configuration { get; } | |
private void ConfigureLogging() | |
{ | |
var loggerConfig = new LoggerConfiguration() | |
.ReadFrom.Configuration(Configuration); | |
Log.Logger = loggerConfig.CreateLogger(); | |
} | |
} |
- After that you can specify your configuration inside your appsettings.json:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Serilog": { | |
"Using": [ "Serilog.Sinks.Seq" ], | |
"MinimumLevel": { | |
"Default": "Information", | |
"Override": { | |
"Microsoft": "Warning", | |
"System": "Warning" | |
} | |
}, | |
"WriteTo": [ | |
{ | |
"Name": "Seq", | |
"Args": { "serverUrl": "http://localhost:5341" } | |
} | |
], | |
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], | |
"Properties": { | |
"Application": "AppName", | |
"EnvironmentName": "Dev" | |
} | |
} |