tags:

views:

42

answers:

2

I obviously don't understand log4net. My root log level is configured as follows:

<root>
   <level value="ERROR"/>
   <appender-ref ref="FileAppender" />
</root>

My log initialization looks as follows:

private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And my actual logging call looks as follows:

LOG.Error("Error submitting Registration.", exc);

Here's the funny part. This log statement ONLY works if I set the root log level to INFO. Why does the log level ERROR not work (and I only want errors, not Info, Debug etc...)?

A: 
<root>
  <level value="ALL" />
...
</root>

Someone of my log4net-Configfile:

<appender name="FileAppender_FullLog" type="log4net.Appender.FileAppender">
  <file value="Log_Full.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
</appender>

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  ..
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_FullLog" />
  <appender-ref ref="FileAppender_SmallLog" />
  <appender-ref ref="SmtpAppender" />
</root>

Edit:

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_SmallLog" />
</root>

The better way ist to use "< filter type="log4net.Filter.LevelRangeFilter">..." because you can change this setting for every appender if you want this in feature.

Floyd
Thanks floyd. Trouble is I actually DONT want all the logs. I only want Error logs, but that log level does not seem to work.
willem
I have edit my post (see "Edit:")
Floyd
A: 

With the help of our clever architect I figured this one out...

I used to have my log initialization in assemblyInfo.cs (but this was wrong).

I moved the log initialization to my global.asax in app_startup:

    protected void Application_Start(Object sender, EventArgs e) 
    {
        string configFilePath = Server.MapPath("~/Config/Log4Net.config");
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
    }

The server.MapPath bit is very important, otherwise log4net tries to read its configuration from the system32\inetsrv folder.

I think my previous logging worked from time to time due to the order in which the assemblies were loaded, or something like that. Either way, this solution works.

willem