4

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?

4

1 回答 1

3

我不相信 Owin 提供了static使丰富工作所需的环境 ( ) 上下文信息。出于这个原因,我LogContext个人首先会选择版本。

于 2015-09-04T11:15:12.553 回答