Log file is coming blank after error in Log4Net
-
26-09-2019 - |
Question
I want to use Log4Net in my asp.net application & I have done the following things:
In Web.Config File
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<logger name="File">
<level value="All" />
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Logs\\Log4Net.log"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
</logger>
</log4net>
In Global.asax
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.Configure();
}
and on Page_load
try
{
// There is no such Session, this is just to create error
if (Session["userName"].ToString() == "Admin")
{
}
}
catch(Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger("File");
}
I have added the reference too in my BIN folder.
When I run my code, I got the error as per the expectation and after that new folder is created in my solution explorer with Log File. But the log file is empty.
I have no guess that why it happened, is it not able to track or record the error??
Please help. Thanks in advance.
Solution
As leppie pointed out, there seems not to be any code that writes a log message. Your excpetion handler should propably look like this:
catch(Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger("File");
logger.Error("something went wrong", ex);
}
The configuration you posted also seems incomplete. You need a log4net
section (cf. example configurations).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow