这是一个完整的解决方案
只需确保使用您的命名空间和相应的程序集名称。
using Microsoft.WindowsAzure.Diagnostics;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace MyNamespace
{
/*
Solves the Azure In-Role Cache client warnings bug which is too verbose in the WAD logs
Also Solves Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener not using Filter
For roles which uses in-role caching, configure your Web.config or app.config with the following system.diagnostics listner and filter:
<system.diagnostics>
<trace>
<listeners>
<add name="AzureDiagnostics" type="MyNamespace.FilteringDiagnosticMonitorTraceListener, MyAssemblyName">
<!-- WARNING: does not work with type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
because the DiagnosticMonitorTraceListener does not call the filter's ShouldTrace method as is was supposed to... -->
<!-- Note: working with type="System.Diagnostics.ConsoleTraceListener" -->
<filter type="MyNamespace.SuppressCacheClientWarningsTraceFilter, MyAssemblyName" initializeData="Information"/>
<!-- Note: The attribute initializeData is set to the text from System.Diagnostics.SourceLevels enum. -->
</add>
</listeners>
</trace>
</system.diagnostics>
*/
/// <summary>EventTypeFilter which suppress the 'noise' messages from the In-Role Azure Cache client
/// </summary>
/// <remarks>It's a workaround for the following problem http://social.msdn.microsoft.com/Forums/windowsazure/en-US/7ebbc44e-7b61-4bbe-aa54-a85a7788079f/complaint-add-hard-complaint?forum=windowsazuredata.
/// The solution is based on http://stackoverflow.com/questions/16443856/how-to-suppress-azure-cache-client-warnings-from-the-wad-logs and http://pastebin.com/qKc1aTTW
/// </remarks>
public class SuppressCacheClientWarningsTraceFilter : EventTypeFilter
{
public SuppressCacheClientWarningsTraceFilter(SourceLevels level)
: base(level) { }
public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] args, object data1, object[] data)
{
return !(
(eventType == TraceEventType.Information && Regex.IsMatch(formatOrMessage, @"^INFORMATION:\ <(CASClient|Complaint)>"))
|| (eventType == TraceEventType.Warning && Regex.IsMatch(formatOrMessage, @"^WARNING:\ <SimpleSendReceiveModule>\ DeadServerCallback"))
);
//return !Regex.IsMatch(formatOrMessage, @"^INFORMATION: <[^>*]*>");
}
}
/// <summary>Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener which uses the configured Trace Filter
/// </summary>
/// <remarks>It's a workaround for the following problem http://social.msdn.microsoft.com/Forums/en-US/92ed1175-d6b7-4173-a224-0f7eb3e99481/diagnosticmonitortracelistener-ignors-filter?forum=windowsazuretroubleshooting
/// The solution is based on the thread comment from "Qin Dian Tang - MSFT": "If you need to use trace filter, then it is needed to use a custom trace listener which derives from DiagnosticMonitorTraceListener, override TraceData, and either manually check filters or call the root class's (TraceListener) TraceData."
/// </remarks>
public class FilteringDiagnosticMonitorTraceListener : DiagnosticMonitorTraceListener
{
public FilteringDiagnosticMonitorTraceListener() : base() { }
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
if (this.Filter == null || this.Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
base.TraceEvent(eventCache, source, eventType, id, format, args);
}
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
{
if (this.Filter == null || this.Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
base.TraceEvent(eventCache, source, eventType, id, message);
}
}
}
希望能帮助到你。