Configuring Log4Net in a web application

I have this code and configuration file below:

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

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

Any idea?

Thank,

Configuration 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> 
+51
c # log4net
Apr 18
source share
5 answers

You need to call the Configure XmlConfigurator function

 log4net.Config.XmlConfigurator.Configure(); 

A call before the first call to loggin or in your Global.asax as follows:

 protected void Application_Start(Object sender, EventArgs e) { log4net.Config.XmlConfigurator.Configure(); } 
+97
Apr 18 2018-12-12T00:
source share

Another way to do this is to add this line to the web application build information:

 // Configure log4net using the .config file [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

Similar to Shriek's.

+18
Apr 18 '12 at 7:05
source share

1: add the following line to the AssemblyInfo class

 [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

2: Make sure you are not using the .Net Framework 4 Client Profile as the Target Framework (I think this is fine on your side, because otherwise it wonโ€™t even compile)

3: Make sure you log in very early. Otherwise, in some scenarios it will not be initialized properly (more about the log4net FAQ ).

So, register something during application launch in Global.asax

 public class Global : System.Web.HttpApplication { private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Global)); protected void Application_Start(object sender, EventArgs e) { Log.Info("Startup application.") } } 

4: Make sure that you have permission to create files and folders on the specified path (if the folder itself also does not exist)

5: The rest of your information looks fine

+6
Apr 18 '12 at 7:01
source share

often this is due to a lack of permissions. The Windows account running the local IIS application pool may not have write access to the application directory. You can create a directory somewhere, give everyone permission to write to it, and point your log4net configurator to that directory. If then a log file is created there, you can change the permissions for your desired log directory so that the application pool can write to it.

Another reason could be uninitialized log4net. In a winforms application, you usually configure log4net when the application starts. In a web application, you can do this either dynamically (in your logging component, check if you can create a specific logger using its name, if not โ†’ call configure ()) or again when you run the application in global.asax.cs .

+2
Apr 18 2018-12-12T00:
source share

I also had a similar problem. No logs were created.

Please verify that the log attribute name must match your LogManager.GetLogger ("name")

 <logger name="Mylog"> <level value="All"></level> <appender-ref ref="RollingLogFileAppender" /> </logger> private static readonly ILog Log = LogManager.GetLogger("Mylog"); 
0
Jun 20 '17 at 15:42
source share



All Articles