Log4net 不会在日志文件中写入日志 [英] Log4net does not write the log in the log file

查看:35
本文介绍了Log4net 不会在日志文件中写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Log4net 创建了一个简单的场景,但我的日志附加程序似乎不起作用,因为消息没有添加到日志文件中.

I have created a simple scenario using Log4net, but it seems that my log appenders do not work because the messages are not added to the log file.

我在 web.config 文件中添加了以下内容:

I added the following to the web.config file:

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

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <file value="D:MyDataDesktopLogFile.txt" />
            <appendToFile value="true" />
            <encoding value="utf-8" />
            <layout type="log4net.Layout.SimpleLayout" />
    </appender>


    <root>
        <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

在我添加的全局ASAX文件中:

Within the global ASAX file I have added:

ILog logger = LogManager.GetLogger(typeof(MvcApplication));

在 Application_Start 方法中:

And within the Application_Start method:

logger.Info("Starting the application...");

为什么测试日志正在启动应用程序..."没有被添加到日志文件中?

Why the test log "Starting the application..." is not being added to the log file?

推荐答案

你打电话了吗

log4net.Config.XmlConfigurator.Configure();

有什么地方可以让 log4net 读取您的配置?例如.在 Global.asax 中:

somewhere to make log4net read your configuration? E.g. in Global.asax:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    // Initialize log4net.
    log4net.Config.XmlConfigurator.Configure();
}

这篇关于Log4net 不会在日志文件中写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆