Wednesday 21 September 2016

Log4Net is not logging the file, randomly stops resolved

In this post we are going to see some important things in the Log4Net logging utility, normally Log4Net is using for logging the data in application, sometimes you may notice that the logging stops randomly, you know why it is happening, surely not, it stops with out any information for that kind of scenario, how we can find the root of the issue, Let me guide you how to do that 



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>


From the above you may notice that we are adding one new key in the app setting which makes the Log4Net to run in Debug mode, this makes to write everything happening behind the scene to do that we have to configure the path to write the debug info.so use the below code snippet configuration



<configuration>
    ...
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="D:\logging\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
    ...
</configuration>


From the above configuration we are writing the debug information in the location, please make sure that the path must be in write permissions

For web applications find the account under which it is running may be IIS_IUSRS, IIS_WPG, or under specific account,then now go to the folder path where you are writing log, do the following steps to give permission.


1. Select the folder, right click and click on properties
2. Select the security tab
3. Click on Edit , under users or group
4. Click Add on security
5. click Advance button
6. Click Find Now






\
















Now you will get a list of users find the users like IIS_IUSR, or IIS_WPG or specific account under you run ur application, and give OK. thats it



Sample configuration file for Log4Net with out Debug:


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

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\log\\TLog.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>




After done this we have to load the information on startup or in load of application

protected void Application_Start(Object sender, EventArgs e) {
   log4net.Config.XmlConfigurator.Configure();
}



From this post you can learn how to debug the Log4Net loggig issue when it randomly stops.


No comments:

Post a Comment