Skip to content

Instantly share code, notes, and snippets.

@michalkutil
Created October 4, 2012 11:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalkutil/3833100 to your computer and use it in GitHub Desktop.
Save michalkutil/3833100 to your computer and use it in GitHub Desktop.
System.Diagnostics Trace listener DotNetOpenAuthFilter.
namespace Common.Log
{
using System.Diagnostics;
using System.Linq;
/// <summary>
/// Indicates whether a listener should trace for events from <c>DotNetOpenAuth</c> library based on the event type.
/// </summary>
public class DotNetOpenAuthFilter : EventTypeFilter
{
/// <summary>
/// <c>DotNetOpenAuth</c> namespace.
/// </summary>
private const string DotNetOpenAuthNamespace = "DotNetOpenAuth.";
/// <summary>
/// Initializes a new instance of the <see cref="DotNetOpenAuthFilter" /> class.
/// </summary>
/// <param name="level">The level.</param>
public DotNetOpenAuthFilter(SourceLevels level)
: base(level)
{
}
/// <summary>
/// Determines whether the trace listener should trace the event.
/// </summary>
/// <param name="cache">A <see cref="T:System.Diagnostics.TraceEventCache" /> that represents the information cache for the trace event.</param>
/// <param name="source">The name of the source.</param>
/// <param name="eventType">One of the <see cref="T:System.Diagnostics.TraceEventType" /> values.</param>
/// <param name="id">A trace identifier number.</param>
/// <param name="formatOrMessage">The format to use for writing an array of arguments, or a message to write.</param>
/// <param name="args">An array of argument objects.</param>
/// <param name="data1">A trace data object.</param>
/// <param name="data">An array of trace data objects.</param>
/// <returns>
/// <c>true</c> if the trace should be produced; otherwise, <c>false</c>.
/// </returns>
public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] args, object data1, object[] data)
{
if (!this.IsDotNetOpenAuthCall())
{
return true;
}
return base.ShouldTrace(cache, source, eventType, id, formatOrMessage, args, data1, data);
}
/// <summary>
/// Determines whether StackTrace contains TraceLogger class from <c>DotNetOpenAuth</c> library.
/// </summary>
/// <returns>
/// <c>true</c> if StackTrace contains TraceLogger class from <c>DotNetOpenAuth</c> library; otherwise, <c>false</c>.
/// </returns>
protected virtual bool IsDotNetOpenAuthCall()
{
var stackTrace = new StackTrace();
foreach (var frame in stackTrace.GetFrames())
{
try
{
if (frame.GetMethod().ReflectedType.FullName.StartsWith(DotNetOpenAuthNamespace))
{
return true;
}
}
catch
{
}
}
return false;
}
}
}
<configuration>
<system.diagnostics>
<trace>
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" >
<filter type="Common.Log.DotNetOpenAuthFilter, Common" initializeData="Warning" />
</add>
</listeners>
</trace>
</system.diagnostics>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment