Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net Logging Debug.WriteLine and Console.WriteLine

I recently came accross log4net thanks to a user here. Anyways, I am wanting to log every Console.Write() and Debug.Write() automatically to the single log file I have configured. Also, I use many class libraries that also have Console/Debug statements that know nothing about log4net. Can these also be logged to the log file automatically based on the configuration? Is any of this possible?

What I want logged in the rolling log file:

Console.WriteLine("console statement");
Debug.WriteLine("debug statement");

Instantiate Logger:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static void InitializeLogger()
{
    XmlConfigurator.Configure();
}

Entire app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="SubToolsPortServerTest.log"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>
like image 374
juicebyjustin Avatar asked May 30 '13 14:05

juicebyjustin


People also ask

Where are log4net logs stored?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.

How do I use log4net net 6?

Just create a log4net. config file with a log file as an appender, then add two using statements and a single line of code to the new . NET 6 hosting model: //Program.


1 Answers

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.

like image 150
Joe Avatar answered Sep 18 '22 17:09

Joe