stevejgordon / CorrelationId

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

Question: Serilog Sink #81

Closed steveoh closed 4 years ago

steveoh commented 4 years ago

Do you know what it would take to add this as a serilog sink so I can update my logging template to include the correlation id without having to modify every log statement to include the id?

stevejgordon commented 4 years ago

Are you using the Microsoft ILogger interface? If so, you can enable the inclusion of the correlation ID on the logging scope in the options. Those objects on the logger scope are structured data in Serilog by default.

steveoh commented 4 years ago

I am using that interface. If I am using an output template like

[{Level:u3} {Timestamp:HH:mm:ss}] ({SourceContext:l}) {Message:lj} {NewLine}{Exception}

Is there a token I could use for the correlation id? Something like...

[{Level:u3} {Timestamp:HH:mm:ss}] ({SourceContext:l})[{CorrelationId}] {Message:lj} {NewLine}{Exception}
stevejgordon commented 4 years ago

It'll only show up in structured log data if you output to something that supports the fully structured data such as ElasticSearch for example. For console messages, and including it in your template, I think you'll need to write an enricher to load the correlation ID. Data from enrichers can then be formatted into your message template (I think). I know we used to do this but we stopped as we found it wasn't that useful on text-based logs to include the correlation ID. We now ship our logs to a structured log store so we can search and aggregate by the correlation ID from there.

steveoh commented 4 years ago

I'll have to check if the id is automatically sent since I am logging to stackdriver with the AddToLoggingScope property true. From what you are saying, it is likely.

steveoh commented 4 years ago

it's working great. thanks!