我们使用 DiagnosticMonitorTraceListener 作为通用跟踪监听器(主要用于 ASP.NET 运行状况监控)以及用于异常处理的 Enterprise Library 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 的一般用途,但不适用于我们具有以下硬编码配置的企业库:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
<add name="Azure Diagnostics Trace Listener" />
</listeners>
</add>
</categorySources>
如果不解决,将生成对 ExceptionPolicy.HandleException 的调用:
Not running in a hosted service or the Development Fabric.
要根据应用程序的运行位置有条件地删除它,我们可以使用 EL5 的流畅配置 API,但必须重写我们的配置(要么全有,要么全无)。
我们还可以使用 web.config 转换,除了已经有 3 种不同的解决方案配置(例如,开发、登台、生产)之外,我们还必须引入第四种来区分 dev-standalone 与 dev- azure 。
最后一个选项是创建一个自定义监听器,它将所有消息路由到 ** **(如果在 Azure 上运行)或不执行任何操作。
还有其他建议吗?
仅供引用,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>
最佳答案
您可以创建一个 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());
}
}
关于asp.net - 在 Azure 计算模拟器外部使用 DiagnosticMonitorTraceListener 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6761693/