Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created June 1, 2012 18:09
Show Gist options
  • Save mikeobrien/2854102 to your computer and use it in GitHub Desktop.
Save mikeobrien/2854102 to your computer and use it in GitHub Desktop.
Trace Listener
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation;
using System.Diagnostics;
using System.Management.Instrumentation;
namespace Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners
{
/// <summary>
/// WmiTraceListener is a <see cref="T:System.Diagnostics.TraceListener"/> that send a WMI event
///
/// </summary>
public class WmiTraceListener : TraceListener, IInstrumentationEventProvider
{
private LoggingInstrumentationProvider instrumentationProvider;
/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.WmiTraceListener"/>
/// </summary>
public WmiTraceListener()
{
this.instrumentationProvider = new LoggingInstrumentationProvider();
}
/// <summary>
/// Sends an event given a predefined string
///
/// </summary>
/// <param name="message">The string to write as the event</param>
public override void Write(string message)
{
Instrumentation.Fire((object) new LogEntry()
{
Message = message
});
}
/// <summary>
/// Sends an email message given a predefined string
///
/// </summary>
/// <param name="message">The string to write as the email message</param>
public override void WriteLine(string message)
{
this.Write(message);
}
/// <summary>
/// Delivers the trace data as an event.
///
/// </summary>
/// <param name="eventCache">The context information provided by <see cref="N:System.Diagnostics"/>.</param><param name="source">The name of the trace source that delivered the trace data.</param><param name="eventType">The type of event.</param><param name="id">The id of the event.</param><param name="data">The data to trace.</param>
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if (this.Filter != null && !this.Filter.ShouldTrace(eventCache, source, eventType, id, (string) null, (object[]) null, data, (object[]) null))
return;
if (data is LogEntry)
{
Instrumentation.Fire((object) (data as LogEntry));
this.instrumentationProvider.FireTraceListenerEntryWrittenEvent();
}
else if (data is string)
this.Write(data);
else
base.TraceData(eventCache, source, eventType, id, data);
}
/// <summary>
/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
/// Returns the object that provides instrumentation services for the trace listener.
///
/// </summary>
/// <see cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.IInstrumentationEventProvider.GetInstrumentationEventProvider"/>
/// <returns>
/// The object that providers intrumentation services.
/// </returns>
public object GetInstrumentationEventProvider()
{
return (object) this.instrumentationProvider;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment