TLDR: You are likely looking for setting up an outputTemplate in your logger configuration:
Log.Logger = loggerConfiguration
.Enrich.FromLogContext() //remember this!
.WriteTo
.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Properties:j}{Message:lj}{NewLine}{Exception}")
.CreateLogger();
or perhaps you forgot to call .Enrich.FromLogContext()
.
Show log properties in console output
I was testing something locally and all I got as output when creating a log event was the timestamp, log level and message. My loggerConfiguratoin looked like the following:
Log.Logger = loggerConfiguration
.Enrich.FromLogContext()
.WriteTo
.Console()
.CreateLogger();
and the way I was logging looked like the following, a simple PushProperty
call:
WeatherForecast[] forecast = GetForecast(summaries);
using (LogContext.PushProperty("LengthOfForecast", forecast.Length))
{
Log.Logger.Information("Forecast created!");
}
This created the following log entry:
[10:08:23 INF] Forecast created!
But, what I would like to see was my log event property "LengthOfForecast"
and its value. In order to see the log event properties, I had to change the output template of the loggerConfiguration
:
Log.Logger = loggerConfiguration
.Enrich.FromLogContext()
.WriteTo
.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Properties:j}{Message:lj}{NewLine}{Exception}") //this!
.CreateLogger();
After this change the output to the console would look like the following:
[10:12:34 INF] {"LengthOfForecast": 5, "RequestId": "0HMSJ60RP8TJ0:00000009", "RequestPath": "/weatherforecast"}Forecast created!
That is all
You can see this post for a full example of the above.
That is all there is to it. This can catch you off guard if you are new to serilog and leave you wondering why your properties are not showing up in the output! To begin with you might think you have mis-configured something.
I hope this helps someone out there! :)