4

When I compiled my application in release mode, I found that the Log4Net still logs debug information; any idea how to fix this?

This is my App.Config file:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="C:\Documents and Settings\test\Application Data\Log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} - %m%n" />
      </layout>
    </appender>
  </log4net>

Did I miss anything?

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • Quite some overlap to this: http://stackoverflow.com/questions/641240/changing-the-log4net-root-level-when-app-is-built-as-release – svrist Jan 04 '11 at 14:49

3 Answers3

14

There's nothing in your App.Config file to tell log4net to do things differently in release or debug mode. If you want logging to be different between the two builds, you have to change your configuration file between the two builds.

Your best bet is probably to create one App.Config for Release, one for Debug, and then follow the advice in the StackOverflow question:

NOTE: The difference between your release and debug App.Config will be the following line in the debug version

<level value="DEBUG" />

versus the following line in the release version (or of course you could choose ERROR or FATAL if you want):

<level value="INFO" />
Community
  • 1
  • 1
Eddie
  • 53,828
  • 22
  • 125
  • 145
4

Maybe try something like this instead? Set to whatever minimum level you want to receive.

<level value="WARN" />
Aaron
  • 2,427
  • 4
  • 30
  • 41
1

If your App.Config looks like this:

    <root>
      <level value="Info" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingFileAppender" />
    </root>

You can modify the log level by code (put the code in Program.cs):


#if DEBUG
            log4net.Repository.ILoggerRepository RootRep;
            RootRep = LogManager.GetRepository(Assembly.GetCallingAssembly());

            XmlElement section = ConfigurationManager.GetSection("log4net") as XmlElement;

            XPathNavigator navigator = section.CreateNavigator();

            XPathNodeIterator nodes = navigator.Select("root/level");
            foreach (XPathNavigator appender in nodes)
            {
                appender.MoveToAttribute("value", string.Empty);
                appender.SetValue("Debug");
            }

            IXmlRepositoryConfigurator xmlCon = RootRep as IXmlRepositoryConfigurator;
            xmlCon.Configure(section);
#endif

wez
  • 11
  • 1
  • 1