Microsoft.Extensions.Logging
Seq fully supports the advanced structured logging provided by .NET Core and ASP.NET Core.
This page describes Seq.Extensions.Logging, a provider for Microsoft.Extensions.Logging with simple loggingBuilder.AddSeq()
configuration.
We recommend using Serilog with Seq when targeting ASP.NET Core. Check out the excellent introductory video C# Logging with Serilog and Seq by Tim Corey, to see ASP.NET Core, Serilog, and Seq working seamlessly together.
Getting started
Add the NuGet package to your project either by editing the CSPROJ file, or using the NuGet package manager:
Install-Package Seq.Extensions.Logging
In your Startup
class's ConfigureServices()
method, call AddSeq()
on the loggingBuilder
provided by AddLogging()
.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddSeq();
});
// ...
The framework injects ILogger
instances into controllers and other classes:
class HomeController : Controller
{
readonly ILogger<HomeController> _log;
public HomeController(ILogger<HomeController> log)
{
_log = log;
}
public IActionResult Index()
{
_log.LogInformation("Hello, world!");
}
}
Log messages will be sent to Seq in batches and be visible in the Seq user interface. Observe that correlation identifiers added by the framework, like RequestId
, are all exposed and fully-searchable in Seq.
Logging with message templates
Seq supports the templated log messages used by Microsoft.Extensions.Logging. By writing events with named format placeholders, the data attached to the event preserves the individual property values.
var fizz = 3, buzz = 5;
log.LogInformation("The current values are {Fizz} and {Buzz}", fizz, buzz);
This records an event like:
Property | Value |
---|---|
Message | "The current values are 3 and 5" |
Fizz | 3 |
Buzz | 5 |
Seq makes these properties searchable without additional log parsing. For example, a filter expression like Fizz < 4
would match the event above.
Additional configuration
The AddSeq()
method exposes some basic options for controlling the connection and log volume.
Parameter | Description | Example value |
---|---|---|
apiKey | A Seq API key to authenticate or tag messages from the logger | "1234567890" |
levelOverrides | A dictionary mapping logger name prefixes to minimum logging levels | new Dictionary<string,LogLevel>{ ["Microsoft"] = LogLevel.Warning } |
minimumLevel | The level below which events will be suppressed (the default is Information ) | LogLevel.Trace |
JSON configuration
The Seq server URL, API key and other settings can be read from JSON configuration if desired.
In appsettings.json
add a "Seq"
property:
{
"Seq": {
"ServerUrl": "http://localhost:5341",
"ApiKey": "1234567890",
"MinimumLevel": "Trace",
"LevelOverride": {
"Microsoft": "Warning"
}
}
}
And then pass the configuration section to the AddSeq()
method:
loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
Updated almost 3 years ago