Question

I am trying to use log4net in an ASP.NET application with Visual Studio 2005. I have declared an instance of the logger like so:

Private Shared ReadOnly log As ILog = LogManager.GetLogger("")

I am trying to use it in the following manner:

If log.IsDebugEnabled Then
   log.Debug("Integration Services Constructed")
End If

Here is my configuration:

<log4net>

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

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="..\\logs\\logfile.log"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="1MB"/>
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="FATAL" />
        </filter>
    </appender>

</log4net>

Unfortunately, log.IsDebugEnabled is always false.
How do I configure log4net so that I can log only debug messages?

Was it helpful?

Solution

Before calling LogManager.GetLogger("")

You have to call log4net.Config.XmlConfigurator.Configure(); In an ASP.NET app you probably want to put this call in Application_Start

OTHER TIPS

Yes, do it like Anson said. Also, if you are calling Configure in a class library you can do that by adding an attribute to your class:

[assembly: XmlConfigurator(Watch = true)]

and if you're using log4net.config file, use it like that instead:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

If you are using a separate configuration file for log4net, do this: after following all the other setup instructions, make sure that u right click on the file in the visual studio solution explorer, select properties, expand the "Advanced" option group, set the "Copy To Output Directory" value as "Copy always". That will do the magic... :) cheers!!

If you are setting log4net up in code rather than in a config file, you can call log4net.Config.BasicConfigurator.Configure before GetLogger.

VB.NET -

<Assembly: log4net.Config.XmlConfigurator(Watch:=True)> 

Use this in any method before you use log :

log4net.Config.XmlConfigurator.Configure();

In App.Config ,the settings should be :

<root>
      <level value="ALL" />
      <appender-ref ref="AppenderName" />
    </root>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top