我们使用DiagnosticMonitorTraceListener作为一般跟踪侦听器(主要用于ASP.NET健康监测)以及用于异常处理的Enterprise 5侦听器。这在Azure上运行的时候很好,但重要的是我们能够在Azure之外运行这个网站,并且只需很少的改动。
一种选择是按以下方式动态注册:
protected void Application_Start()
{
if (Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable)
{
System.Diagnostics.Trace.Listeners.Add(new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener());
System.Diagnostics.Trace.AutoFlush = true;
}
}这适用于ASP.NET健康监测和System.Diagnosics的一般用途,但不适用于企业库( Enterprise ),因为我们有以下硬编码配置:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
<add name="Azure Diagnostics Trace Listener" />
</listeners>
</add>
</categorySources>如果没有寻址,对ExceptionPolicy.HandleException的调用将生成:
不在宿主服务或开发结构中运行。
为了根据应用程序的运行位置有条件地删除它,我们可以为EL5使用fluent配置API,但是必须重写我们的配置(全部或者没有)。
我们也可以使用web.config转换,但除了已经有3种不同的解决方案配置(例如,dev、暂存、生产)之外,我们还必须引入第4种方法来区分dev-独立与dev-azure。
最后一个选项是创建一个自定义监听器,它要么将所有消息路由到*(如果运行在Azure上),要么什么也不做。
还有其他建议吗?
FYI,ASP.NET健康监测配置如下:
<healthMonitoring enabled="true">
<providers>
<add name="TraceWebProvider" type="System.Web.Management.TraceWebEventProvider" />
</providers>
<rules>
<add name="Application Events"
eventName="Application Lifetime Events"
provider="TraceWebProvider"
profile="Default"
minInstances="1"
maxLimit="Infinite"
minInterval="00:01:00" />
</rules>
</healthMonitoring>发布于 2012-01-06 01:08:40
您可以创建一个DiagnosticMonitorTraceListener,然后将其添加到类别的TraceSources集合中。
从EntLib配置中删除Azure诊断跟踪侦听器:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
</categorySources>然后使用下面的代码在运行时添加它:
protected void Application_Start()
{
if (Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable)
{
LogSource logSource;
Logger.Writer.TraceSources.TryGetValue("General", out logSource);
logSource.Listeners.Add(new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener());
}
}https://stackoverflow.com/questions/6761693
复制相似问题