How do I set up the mix? I have no problems configuring Application Insights and NLog but I have no idea how to correlate operations. I use latest version of NLog so it's aware of System.Diagnostics.Trace.CorrelationManager.ActivityId
with its ${activityid}
variable. On the other hand Application Insights uses it's own correlation mechanism. My questions are:
- Who is responsible to initialize standard
Trace.CorrelationManager.ActivityId
? I thought it was ASP.NET MVC but in the debugger it's alwaysGuid.Empty
. If it's up to me where is the best place in MVC pipeline to generate the id? - How to make Application Insights use
Trace.CorrelationManager.ActivityId
? Or, make NLog use Aplication Insights' internal correlation ID? - How to make sure the ID is properly propagated/restored on any
Task.Run()
andawait
calls?
Update:
Here is what I ended up with to link AI to NLog:
private void Log(LogEventInfo lei)
{
lei.Properties["OperationId"] = CorrelationManager.GetOperationId();
this.logger.Log(lei);
}
This is wrapper over NLog's Log()
method which adds a property that can be referenced in NLog.config as ${event-context:OperationId}
. CorrelationManager
here is the solution from the link provided by @Aravind. The use of system CallContext
guarantees that operation Ids will flow across all async points. Now, we need to grab AI operation id and store it in CorrelationManager
. This is done in Global.asax.cs
:
protected void Application_BeginRequest()
{
RequestTelemetry telemetry = HttpContext.Current.GetRequestTelemetry();
string operationId = telemetry?.Id ?? Guid.NewGuid().ToString();
CorrelationManager.SetOperationId(operationId);
}
Now, If AI is enabled for your application your NLog log is correlated with AI log.