4

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:

  1. Who is responsible to initialize standard Trace.CorrelationManager.ActivityId? I thought it was ASP.NET MVC but in the debugger it's always Guid.Empty. If it's up to me where is the best place in MVC pipeline to generate the id?
  2. How to make Application Insights use Trace.CorrelationManager.ActivityId? Or, make NLog use Aplication Insights' internal correlation ID?
  3. How to make sure the ID is properly propagated/restored on any Task.Run() and await 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.

UserControl
  • 14,766
  • 20
  • 100
  • 187

1 Answers1

1

If you're using Application Insights 2.1+ in your .net core application (probably .net framework too but I've not tested) they automatically set System.Diagnostics.Activity.Current to an object that has all the Application Insights info you could want and more (ref).

Within code you can use System.Diagnostics.Activity.Current?.Id or System.Diagnostics.Activity.Current?.RootId. (Use the debugger to inspect Activity.Current to see what else is there & understand what the different nlog tags will output)

To log it using nlog you use the NLog.DiagnosticSource package:

  1. Install the package

    Install-Package NLog.DiagnosticSource or in your csproj:

    <PackageReference Include="NLog.DiagnosticSource" Version="1.*" />
    
  2. Add to your nlog.config:

    <extensions>
        <add assembly="NLog.DiagnosticSource"/>
    </extensions>
    
  3. Add to an nlog Target:

Use ${activity:property=TraceId} (or Id instead of TraceId, or one of the many other properties they list) in a <target>, e.g.:

<extensions>
    <add assembly="NLog.DiagnosticSource"/>
</extensions>
<targets>
    <target name="console" xsi:type="console" layout="${message}|TraceId=${activity:property=TraceId}" />
</targets>
<rules>
    <logger minLevel="Info" writeTo="console" />
</rules>

This will output log messages something like:

My log message|TraceId=921af8f6ba994c9eb3832ebf200846d7

or using Id instead of TraceId

My log message|Id=00-921af8f6ba994c9eb3832ebf200846d7-0e8ba5571915864b-00
Rory
  • 40,559
  • 52
  • 175
  • 261