We're using DiagnosticMonitorTraceListener as a general trace listener (primarily for for ASP.NET Health Monitoring) as well as an Enterprise Library 5 listener for exception handling. This works well when running on Azure but it's important that we be able to run the website outside of Azure with minimal changes.
One option is to register it dynamically as follows:
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;
}
}
This works for ASP.NET Health Monitoring and general uses of System.Diagnosics but not for Enterprise Library where we have the following hard-coded configuration:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
<add name="Azure Diagnostics Trace Listener" />
</listeners>
</add>
</categorySources>
Left unaddressed, calls to ExceptionPolicy.HandleException will generate:
Not running in a hosted service or the Development Fabric.
To conditionally remove this based on where the app is running, we could use the fluent configuration API for EL5 but would have to rewrite our configuration (it's all 开发者_如何学Cor nothing).
We could also use web.config transformations except that, in addition to having 3 different solution configurations already (e.g., dev, staging, production), we would have to introduce a 4th to differentiate between dev-standalone vs. dev-azure.
One last option would be to just create a custom listener that will either route all messages to ** ** (if running on Azure) or do nothing.
Any other suggestions?
FYI, ASP.NET Health Monitoring is configured as follows:
<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>
You can create a DiagnosticMonitorTraceListener and then add it to the collection of TraceSources for your Category.
Remove Azure Diagnostics Trace Listener from EntLib configuration:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
</categorySources>
And then use the code below to add it at runtime:
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());
}
}
精彩评论