I'm new to both Serilog and Seq, and I'm trying to format the logs injested in Seq in a specific way.
I've configured two sinks, one for the console and one for seq, in my appsettings file. For my console everything works as expected, and the ingested logs follow the provided format, but my Seq sink seems to keep using the default one.
I have a hard time figuring out the Seq specific documentation for Serilog to figure out what's wrong. I'm suspecting outputTemplate is not the setting I should be using.
my current setup:
Extension method to configure Serilog.
builder.Host.UseSerilog((context, config) =>
{
config.ReadFrom.Configuration(context.Configuration);
});
Appsettings.json
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.Seq",
"Serilog.Expressions"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "http://logging-seq:5341",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
What am I doing wrong for the Seq sink specifically?
I've tried various other solutions I found on this website, including the following WriteTo configuration for Seq:
{
"Name": "Seq",
"Args": {
"serverUrl": "http://logging-seq:5341",
"formatter": {
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
}
}
}
The ingested logs were still in the default format. The Seq sink seems to be working differently from the Console one.
Formatting of this kind only works for text-based sinks; the Seq sink is showing structured data with "columns" for well-known properties like the timestamp, level, and message.
To show a property like SourceContext
as a top-level column in Seq, click the green tick icon beside the property name and choose "Show as column". You can save the resulting signal (preconfigured view) and set this as a default in your user preferences if required.
There is an API to customize message formatting for the Seq sink, through WriteTo.Seq()
's formatter
argument, but this is not likely to be what you are looking for and because it's controlling the raw JSON format sent to the ingestion endpoint, will most likely break/interfere with other Seq functions if modified.