Writing out SourceContext

147 views
Skip to first unread message

Michael DeMond

unread,
Mar 25, 2016, 5:55:08 AM3/25/16
to Serilog
Hello All,

I am having problems getting the SourceContext to emit properly based on parameter position.

If I do this:
logger.Information( "{Time:ss':'fff} ({Since:ss':'fff}): {Event} {SourceContext}", tracker.Time, tracker.Mark(), @event )

The SourceContext emits as expected.

However, if I do this:
logger.Information( "{Time:ss':'fff} ({Since:ss':'fff}): {SourceContext} {Event}", tracker.Time, tracker.Mark(), null, @event )

or this:
logger.Information( "{Time:ss':'fff} ({Since:ss':'fff}): {SourceContext} {Event}", tracker.Time, tracker.Mark(), @event )

This does not emit as expected.

Is there a way to emit the SourceContext as I am trying to do here?  Or is the only way to get it to work as expected is by the first example?

Thank you,
Michael

nblum...@nblumhardt.com

unread,
Mar 25, 2016, 7:11:10 AM3/25/16
to Serilog
Hi Michael,

Are you using one of the Console or File sinks? If so, they accept an outputTemplate: parameter that you can specify custom formatting in for all events (the logger methods like Information() expect a 1:1 mapping between template parameters and arguments).

Log.Logger = new LoggerConfiguration()
    .WriteTo.ColoredConsole(outputTemplate:
        "{Timestamp:HH:mm} [{Level}] ({SourceContext}) {Message}{NewLine}{Exception}")
    .CreateLogger();

Let me know if you need more info.

Regards,
Nick

Michael DeMond

unread,
Mar 25, 2016, 9:13:23 AM3/25/16
to Serilog
Hmmm... yeah, that might be part of the problem here.  I am using my own "recording" sink that captures LogEvents and then plays them back at a later time to the ITestOutputHelper in xUnit.  xUnit's ITestOutputHelper is not available at the time that I am writing messages so I am storing them.  It would be nice to have some sort of "placeholder" token I can supply in my arguments that would enable Serilog to look it up somehow.  Maybe a new feature request? :)

Thank you,
Michael
Reply all
Reply to author
Forward
0 new messages