CorrelationId icon indicating copy to clipboard operation
CorrelationId copied to clipboard

Add small example snippet for adding CorrelationId to logs

Open svdHero opened this issue 4 years ago • 0 comments

Hi there,

the wiki states

AddToLoggingScope - Add the correlation ID value to the logger scope for all requests. When true the value of the correlation ID will be added to the logger scope payload.

However, I am new to the whole .NET logging framework and I am not familiar with logger scopes. Could you provide a small code snippet that shows how I can get the Correlationid in each entry of my console logger automagically? That would be really great.

So far I set up my logging using

            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddSimpleConsole(options =>
                    {
                        options.TimestampFormat = "'['yyyy'-'MM'-'dd HH':'mm':'ss']' ";
                        options.UseUtcTimestamp = true;
                        options.IncludeScopes = true;
                        options.SingleLine = true;
                    });
                });

All I want is a console logger that logs a line with Timestamp, LogLevel, CategoryName (=LoggerName???), CorrelationId and LogMessage.

I would be really grateful for any example, because the beauty of this middleware seems to be that it is an easier entrypoint for beginners like myself than the whole Activity and logging jungle.

svdHero avatar Jan 20 '21 14:01 svdHero