You can redirect Debug.WriteLine
(in debug builds) and Trace.WriteLine
to log4net by writing a custom TraceListener
that logs to log4net. The salient bits of a simplistic implementation will look something like:
public class Log4NetTraceListener : TraceListener
{
ILog _logger = ...;
public override void WriteLine(string message)
{
_logger.Info(message);
}
}
You would then configure this in your application configuration file something like:
<system.diagnostics>
...
<listeners>
<add name="traceListener" type="MyNamespace.Log4NetTraceListener,MyAssembly" />
</listeners>
...
</system.diagnostics>
A more complete implementation might support configuration of the logger name, the logging level to use, and perhaps some internal buffering of messages.
For Console.WriteLine
you could redirect to your own custom TextWriter
by calling Console.SetOut
, and your custom TextWriter
could log to log4net.