Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Log4Net in web application

Tags:

c#

log4net

I have this code and the config file below:

ILog log = LogManager.GetLogger(typeof(MyClass)); log.Debug("Testing"); 

TestProj directory is not created and if I create it, no TestLog.txt file, no log ... nothing.

Any idea?

Thanks,

The config file

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />  <log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">   <file value="C:\\TestProj\\TestLog.txt" />   <appendToFile value="true" />   <rollingStyle value="Size" />   <maxSizeRollBackups value="10" />   <maximumFileSize value="10MB" />   <staticLogFileName value="true" />   <layout type="log4net.Layout.PatternLayout">     <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />   </layout> </appender>  <root>   <level value="DEBUG" />   <appender-ref ref="RollingLogFileAppender" /> </root> </log4net> 
like image 925
Kris-I Avatar asked Apr 18 '12 06:04

Kris-I


People also ask

Where do I put log4net in web config?

Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.


1 Answers

You need to call the Configurefunction of the XmlConfigurator

log4net.Config.XmlConfigurator.Configure(); 

Either call before your first loggin call or in your Global.asax like this:

protected void Application_Start(Object sender, EventArgs e) {    log4net.Config.XmlConfigurator.Configure(); } 
like image 185
shriek Avatar answered Sep 19 '22 12:09

shriek