stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
559 stars 106 forks source link

Add small example snippet for adding CorrelationId to logs #100

Open svdHero opened 3 years ago

svdHero commented 3 years ago

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.