Custom Trace Listern for EntLib
the current custom trace listerner in the documentation uses debug .write, which doesn't work in the "release" mode.
I would like to show trace message to debugview even in release mode.
OutputDebugString do the magic, see the code below
----
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace TareqCo.Framework.Logging
{
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class DebugTraceListener:Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern void OutputDebugStringA(string message);
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if (data is LogEntry && this.Formatter != null)
{
this.WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
this.WriteLine(data.ToString());
}
}
public override void Write(string message)
{
OutputDebugStringA(message);
}
public override void WriteLine(string message)
{
OutputDebugStringA(message);
}
}
}