Using Serilog with .Net core and App Insights
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:
- Create a Serilog enricher that would record the current
Activity
id as aScalarValue
in aLogEventProperty
- see OperationIdEnricher - [Optional] Create an extension for this enricher - see LoggingExtensions
- Register the enricher / add it to the pipeline via code or config - see logging.json
- Create a custom
TelemetryConverter
(subclass fromTraceTelemetryConverter
orEventTelemetryConverter
) for ApplicationInsights that would settelemetry.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
If you are using ILogger in .Net core for logging purposes, those message can be directed to Application Insights with the following modification of startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
/*...existing code..*/
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
}
If you employ your own correlation ID, you can modify Application Insights correlation IDs accordingly in Context.Operation
field of telemetry item with your own Telemetry Initializer or pass those values in the respective headers (Request-ID
(global ID) and Correlation-Context
(name-value pairs)) in the requests to this app - AI will pick up correlation IDs from those.
The end to end transaction is supposed to be displayed together (Requests/Dependencies and Exceptions) on a timeline in the details view of Application Insights telemetry. With you own correlation IDs it should work as well if they are in there from the very beginning of transaction (e.g. in the first component) - otherwise injecting them in the middle will break the chain.