Pergunta

Is there a way to have Trace.CorrelationManager.ActivityId automatically included as a column in the WADLogsTable when using System.Diagnostics tracing with Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener as the listener?

Foi útil?

Solução

The new versions of Azure .NET SDK include an ActivityId column in the WAD tables, and you can include custom columns by using EventSource derived classes (just verified this on Azure SDK 2.6, although you need to make sure you are using .NET 4.5.1 since .NET 4.5.0 seems to have some bugs that result in silent failures).

For ASP.NET apps, in your Application_BeginRequest method, you would do the following:

protected void Application_BeginRequest()
{
    Guid requestId = Guid.NewGuid();
    System.Diagnostics.Trace.CorrelationManager.ActivityId = requestId;
    System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(requestId);     
}

You can create a custom EventSource class such as the following:

[EventSource(Name="MyCompany-MyProduct-MyEventSource")]
public class MyEventSourceWriter : EventSource
{
    public static MyEventSourceWriter Log = new MyEventSourceWriter();

    public MyEventSourceWriter()
    {
    }

    public void MyEvent(string myValue1, string myValue2, string myValue3)
    {
        if (IsEnabled())
        {
            this.WriteEvent(1, myValue1, myValue2, myValue3);
        }
    }     
}

You would then enable this in your diagnostics.wadcfgx file with something like this:

<EtwEventSourceProviderConfiguration scheduledTransferPeriod="PT1M" provider="MyCompany-MyProduct-MyEventSource">
    <Event id="1" eventDestination="MyEvent" />
    <DefaultEvents />
</EtwEventSourceProviderConfiguration>

Then, if you want to actually write a log entry, you would just do the following from anywhere in your code:

MyEventSourceWriter.Log.MyEvent("Hello", "World", "That is all.");

Then, after the first time a log entry is created for that event (and you wait the appropriate time for Azure Diagnostics to pull the logs), the "WADMyEvent" table will be created, and it will have columns for ActivityId, myValue1, myValue2, and myValue3.

Outras dicas

Unfortunately there is no way to modify the format of the data collected by WAD.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top