I don't think that there is a standard trace listener for routing events
into log4net, but is should be simple to knock one up. The issues are
that from the TraceListener API you can log without specifying a
category, and the listener itself does not know the level you are
logging at. But apart from that you can do something like:


using System.Diagnostics;

namespace TestConsoleApp
{
        public class Log4NetListener : TraceListener
        {
                public override bool IsThreadSafe
                {
                        get { return true; }
                }

                public override void Write(string message)
                {
                        Write((object)message, null);
                }

                public override void Write(object o)
                {
                        Write(o, null);
                }

                public override void Write(object o, string category)
                {
                        // Come up with a useful default logger name
                        if (category == null || category.Length == 0)
                        {
                                category = "Log4NetListener";
                        }
                        log4net.LogManager.GetLogger(category).Info(o);
                }

                public override void Write(string message, string
category)
                {
                        Write((object)message, category);
                }

                public override void WriteLine(string message)
                {
                        Write(message);
                }

                public override void WriteLine(object o)
                {
                        Write(o);
                }

                public override void WriteLine(object o, string
category)
                {
                        Write(o, category);
                }

                public override void WriteLine(string message, string
category)
                {
                        Write(message, category);
                }
        }
}

Cheers.

> -----Original Message-----
> From: Widerberg Marcus [mailto:[EMAIL PROTECTED] 
> Sent: 21 March 2007 14:06
> To: [email protected]
> Subject: System.Trace.Diagnostics to log4net
> 
> Hello,
> 
> I would like to get diagnostics messages (for different wcf 
> services) routed to log4net and then to the log4net appenders.
> 
> Is there a standard way to do this? Basically, I need a 
> tracelistener for the system diagnostics trace system that 
> sends the messages on to log4net. A Log4NetListener.
> 
> Any help appreciated!
> 
> Best regards,
> 
> /mawi
> 

Reply via email to