11

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.

frosty
  • 2,421
  • 6
  • 26
  • 47

2 Answers2

17

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

ironstone13
  • 3,325
  • 18
  • 24
  • Activity.Current is always is null in asp.net mvc web app/api? Does any working code with configuration available? – anomepani Feb 12 '21 at 11:37
4

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.

Dmitry Matveev
  • 2,563
  • 11
  • 16
  • Does Ilogger work this way with Azure functions that use app insights (use request-id in header as correlation id)? – Joe Eng May 16 '18 at 23:38
  • Azure Functions have their own integration with Application Insights, so it may differ slightly, but I'd expect that incoming headers are still parsed to provide an appropriate correlation capabilities. You can ask this question on [Azure Function's GitHub](https://github.com/Azure/Azure-Functions). – Dmitry Matveev May 17 '18 at 22:22
  • ApplicationInsightsLoggerProvider is now enabled by default when enabling ApplicationInsights monitoring using UseApplicationInsights extension method on IWebHostBuilder or AddApplicationInsightsTelemetry extension method on IServiceCollection. From 2.7.0-beta3 onwards, calling this method will result in double logging and filters applied will not get applied. Read more https://aka.ms/ApplicationInsightsILoggerFaq – Enrico Aug 05 '20 at 10:25