Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Serilog with .Net core and App Insights

I have used App insights directly for application logging before and I have seen that .Net core platform also creates trace events that goes to App insights.

In a new .Net core API application, I'd like to use Serilog for application logging and App Insight for storing and visualizing the log events. I'd like to know:

  1. How to continue to get the .Net core .created trace events to App insights?

  2. How can I pass correlation Id from my application to .Net core created trace events?

  3. Will end to end transaction feature in App insight portal show all the events together? It is important for me to know and keep an eye on the latency of SQL calls.

like image 771
frosty Avatar asked Apr 29 '18 16:04

frosty


1 Answers

Simply using Serilog.Sinks.ApplicationInsights is not enough, as it will not correlate Serilog events with the rest of your telemetry on Application Insights.

To correlate the events, so they are shown as one "End-to-End transaction" - you have to do the following things:

  1. Create a Serilog enricher that would record the current Activity id as a ScalarValue in a LogEventProperty - see OperationIdEnricher
  2. [Optional] Create an extension for this enricher - see LoggingExtensions
  3. Register the enricher / add it to the pipeline via code or config - see logging.json
  4. Create a custom TelemetryConverter (subclass from TraceTelemetryConverter or EventTelemetryConverter) for ApplicationInsights that would set telemetry.Context.Operation.Id from value set in 1) - see OperationTelemetryConverter

Check out my blog post "Serilog with ApplicationInsights" that explains the points above with more details, and links

Also, be sure to take a look at Telemetry correlation in Application Insights on MSDN

like image 62
ironstone13 Avatar answered Sep 19 '22 18:09

ironstone13