serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
313 stars 100 forks source link

Can't figure out BeginScope? #65

Closed Capsup closed 7 years ago

Capsup commented 7 years ago

Hi,

I'm having issues with the extension in a .Net Core Console Application, where I can't get the BeginScope function to do anything when using the Serilog extension.

I'm creating the logger like so:

Log.Logger = new LoggerConfiguration().Enrich.FromLogContext().WriteTo.LiterateConsole().CreateLogger();

And using the logger like so (as per #50 ):

var logger = new LoggerFactory().AddSerilog().CreateLogger(type)
var counter = 1;
var threadid = 2;
using( logger.BeginScope( "Test {counter} # {threadid}", counter, threadid ) )
{
    foreach( var registeredMethod in registeredMethods )
    {
        logger.LogInformation( "test | {0}", "abc" );
    }
}

However, I can't seem to get the scope to print anything at all to the console (nor using the flatfile extension): image

What am I doing wrong here?

nblumhardt commented 7 years ago

Howdy! To add any output to the console you'll need to specify an outputTemplate argument to LiterateConsole():

    .WriteTo.LiterateConsole(outputTemplate:
        "{Timestamp:o} [{Level:u3}] {Scope} {Message}{NewLine}{Exception}")

The Scope property will show if there are text scope names like yours above; alternatively you can use {threadId} and so-on to show individual values.

Check out: https://nblumhardt.com/2016/11/ilogger-beginscope/

Thread ids are probably better added with: https://github.com/serilog/serilog-enrichers-thread

HTH!

Capsup commented 7 years ago

Thanks alot. I'm sorry it took me this long to respond, but yes, you're right, I did notice that outputTemplate but didn't consider that it had something to do with it. Thanks alot for your help.

Porges commented 7 years ago

Just ran into this myself. It's a little unexpected that the default behaviour doesn't include {Scope} anywhere. Would it make sense to include it?

nblumhardt commented 7 years ago

@Porges thanks for the feedback.

The Serilog.Extensions.Logging.File package does, because it assumes MEL is on the front-end, but other parts of Serilog are independent of MEL and so scope doesn't really make sense for them.

In the future, more packages like the aforementioned one might help simplify the experience, but we're still in the exploratory phase.

Porges commented 7 years ago

@nblumhardt That makes sense, thanks 👍