I'm trying to figure out what approach is the best:
I have an Owin pipeline where I would like to enrich all logs with various information based on the request (URL, IP address, etc.)
As I see it I have two possibilities with Serilog ForContext()
in the Owin pipeline:
public override async Task Invoke(IOwinContext context)
{
using (LogContext.PushProperties(
RequestUri(context),
RemoteIp(context)))
{
await Next.Invoke(context);
}
}
Or set up enrichers when configuring Serilog:
var configuration = new LoggerConfiguration()
.Enrich.WithRequestUrl()
.Enrich.WithRequestClientIp()
...
Is there a right or wrong way to do this? Personally I like the LogContext as I provides me with an easy way to access the Owin Context, but on the other hand all my requests will trigger all the enrichers to be evaluated. But then again, it is only done once during a request, instead of for each logging statement?
Is this solely opinion-based and should therefore be closed, or is there any advantages to the one approach compared to the other?