Trace.CorrelationManager.ActivityId 作为 WADLogsTable 列 [英] Trace.CorrelationManager.ActivityId as WADLogsTable column
问题描述
当使用 System.Diagnostics
跟踪时,有没有办法让 Trace.CorrelationManager.ActivityId
作为列自动包含在 WADLogsTable
中Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener
作为侦听器?
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?
推荐答案
新版本的 Azure .NET SDK 在 WAD 表中包含一个 ActivityId 列,您可以使用 EventSource 派生类包含自定义列(刚刚在Azure SDK 2.6,但您需要确保您使用的是 .NET 4.5.1,因为 .NET 4.5.0 似乎有一些会导致静默失败的错误).
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).
对于 ASP.NET 应用程序,在您的 Application_BeginRequest 方法中,您将执行以下操作:
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);
}
您可以创建一个自定义的 EventSource 类,如下所示:
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);
}
}
}
然后,您将在您的diagnostics.wadcfgx 文件中启用此功能,如下所示:
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.");
然后,在第一次为该事件创建日志条目后(并等待 Azure 诊断提取日志的适当时间),将创建WADMyEvent"表,其中包含 ActivityId 列,myValue1、myValue2 和 myValue3.
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.
这篇关于Trace.CorrelationManager.ActivityId 作为 WADLogsTable 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!