Log4Net 出错后日志文件变为空白

发布于 2024-09-13 20:38:16 字数 1362 浏览 5 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-09-20 20:38:16

正如 leppie 指出的那样,似乎没有任何代码可以写入日志消息。您的异常处理程序应该如下所示:

catch(Exception ex)
{
    log4net.ILog logger = log4net.LogManager.GetLogger("File");
    logger.Error("something went wrong", ex);
}

您发布的配置似乎也不完整。您需要一个 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:

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文