Logging http header...what am I doing wrong?

643 views
Skip to first unread message

paolo ponzano

unread,
Jan 4, 2016, 10:58:13 AM1/4/16
to Serilog
Hello again,
I'm trying to log an http header that hold's the username (this information is just passed at every call on ServiceStack's headers)

I'm trying to put the logging login on a single point that's servicestack servicerunngers as

<pre>
 public override object Execute(IRequest request, object instance, TRequest requestDto)
 {
        var username = request.Headers.Get(HeaderHelper.UserName);

         if (!string.IsNullOrEmpty(username))
         {
             Serilog.Log.Logger.ForContext(HeaderHelper.UserName, username);
          }
         Serilog.Log.ForContext(instance.GetType()).Debug("[Request] Logging request {@Request}", requestDto, username);

         return base.Execute(request, instance, requestDto);
}
</pre>

using this approach won't log the username field in my template (consider this for now  as template

<add key="serilog:write-to:RollingFile.outputTemplate" value="{Username} - {Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" />)

I've tried to create as well an enricher but I can't find the Username field inside the enricher

public class UsernameEnricher: ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
        //where should I look here ?
        }
    }


Thanks

Ian Yates

unread,
Jan 4, 2016, 9:58:01 PM1/4/16
to Serilog
Hi Paolo,

The "ForContext" calls you're making return a new logger instance which you then need to use.  In your code you're just throwing away the result.  I don't use ServiceStack but perhaps there's a way to wire up how ILogger gets injected into your service classes such that you can inject it with the appropriate context set for you.

I think you may be trying to use this like the LogContext class where, in an async context, you can specify ambient logging data to be captured (and it then needs to be popped off the stack in the correct order too)

You may find https://github.com/serilog-web/classic useful for logging HTTP headers instead :)

Ian
Reply all
Reply to author
Forward
0 new messages