Log4Net 出错后日志文件变为空白
我想在我的 ASP.NET 应用程序中使用 Log4Net我做了以下事情:
Web.Config 文件
<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>
在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();
}
和 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");
}
中,我也在我的 BIN 文件夹中添加了引用。
当我运行代码时,我收到了预期的错误,然后在我的解决方案资源管理器中使用日志文件创建了新文件夹。但日志文件是空的。
我不知道为什么会发生这种情况,是不是无法跟踪或记录错误?
请帮忙。提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 leppie 指出的那样,似乎没有任何代码可以写入日志消息。您的异常处理程序应该如下所示:
您发布的配置似乎也不完整。您需要一个
log4net
部分(参见示例配置< /a>)。As leppie pointed out, there seems not to be any code that writes a log message. Your excpetion handler should propably look like this:
The configuration you posted also seems incomplete. You need a
log4net
section (cf. example configurations).