I am using log4net
Nuget package version 2.0.8
in a Web Application.
Whenever any exception is raised in the application that is logged in log.txt
file. It is working fine in local. However it is not working in server. In server only the date-time value is written into the log file but not the exception details.
Below is the configuration in the Web.config
file:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="DatePattern" value="-yyyy-MM-dd'.txt'" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="true" />
<param name="MaxSizeRollBackups" value="-1" />
<param name="RollingStyle" value="Date" />
<param name="CountDirection" value="1" />
<param name="File" value="D:\Logs\log.txt" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%newline%d %property{log4net:HostName} [%t] %-5p - %m%n" />
</layout>
<threshold value="ERROR"/>
</appender>
<root>
<priority value="All" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
The log4net section is also defined in the Web.config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false" />
</configSections>
The Log object is created in the class level variable as follows:
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
The exception is logged like follows:
try
{
}
catch (Exception ex)
{
Log.Error( "Test exception :" + ex.Message, ex);
}
In server only the date time is written in the log file but not the exception details. See the below image for reference:
In the Global.asax
file the log4net is registered like follows:
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
Can anyone help me how can I resolve this error. Any help is appreciated.
It worked after I changed the log4net
settings in Web.config
file:
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%newline%d %message" />
</layout>
I noticed the error log is written to the file after ~2 minutes
. So don't open the file to see the logs. Wait for ~2 minutes then open.