Log4net不会将日志写入日志文件

发布于 2024-09-17 05:09:46 字数 1074 浏览 12 评论 0原文

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

我将以下内容添加到 web.config 文件中:

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

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <file value="D:\MyData\Desktop\LogFile.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 文件中:

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

在 Application_Start 方法中:

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

为什么测试日志“启动应用程序...”没有添加到日志文件中?

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.

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:\MyData\Desktop\LogFile.txt" />
            <appendToFile value="true" />
            <encoding value="utf-8" />
            <layout type="log4net.Layout.SimpleLayout" />
    </appender>


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

Within the global ASAX file I have added:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

盛夏尉蓝 2024-09-24 05:09:47

另外,请确保为 [log4net].config 选择“始终复制”选项

在此处输入图像描述

Also, Make sure the "Copy always" option is selected for [log4net].config

enter image description here

梦幻之岛 2024-09-24 05:09:47

正如@AndreasPaulsson 所建议的,我们需要配置它。我正在 AssemblyInfo 文件中进行配置。我在此处指定配置文件名

// Log4Net Configuration.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

As @AndreasPaulsson suggested, we need to configure it. I am doing the configuration in AssemblyInfo file. I specify the configuration file name here.

// Log4Net Configuration.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
╄→承喏 2024-09-24 05:09:47

确保运行站点的进程(帐户)具有写入输出目录的权限。

在 IIS 7 及更高版本中,这是在应用程序池上配置的,通常是 AppPool Identity,通常没有写入所有目录的权限。

检查您的事件日志(应用程序和安全性)以查看是否引发任何异常。

Make sure the process (account) that the site is running under has privileges to write to the output directory.

In IIS 7 and above this is configured on the application pool and is normally the AppPool Identity, which will not normally have permission to write to all directories.

Check your event logs (application and security) to see if any exceptions were thrown.

找回味觉 2024-09-24 05:09:47

插入:

 [assembly: log4net.Config.XmlConfigurator(Watch = true)]

AssemblyInfo.cs 文件末尾

Insert:

 [assembly: log4net.Config.XmlConfigurator(Watch = true)]

at the end of AssemblyInfo.cs file

夏见 2024-09-24 05:09:47

就我而言,我必须授予日志文件的 IIS_IUSRS 读\写权限。

In my case I had to give the IIS_IUSRS Read\write permission to the log file.

野侃 2024-09-24 05:09:47

对我来说,我移动了日志文件的位置,只有当我将文件名更改为其他名称时,它才会再次启动。

似乎如果已经存在同名的日志文件,则不会发生任何情况。

之后,我重命名旧文件并将配置中的日志文件名再次更改回原来的名称。

For me I moved the location of the logfiles and it was only when I changed the name of the file to something else it started again.

It seems if there is a logfile with the same name already existing, nothing happens.

Afterwards I rename the old file and changed the log filename in the config back again to what it was.

月棠 2024-09-24 05:09:47

就我而言,由于项目名称中存在空格,log4net 无法正确记录。让我抓狂的是为什么相同的代码在不同的项目中运行得很好,但在新项目中却不起作用。空格。一个简单的空间。

因此,请注意项目名称中的空格。我已经吸取教训了。

In my case, log4net wasn't logging properly due to having a space in my project name. Drove me nuts why the same code worked just fine in a different project, but didn't in the new one. Spaces. A simple space.

So, beware spaces in project names. I've learned my lesson.

深海夜未眠 2024-09-24 05:09:47

对我来说,我必须将 Logger 移至 Nuget 包。需要在 NuGet 包项目中添加以下代码。

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

请参阅 https: //gurunadhduvvuru.wordpress.com/2020/04/30/log4net-issues-when-moved-it-to-a-nuget-package/ 了解更多详细信息。

For me I had to move Logger to a Nuget Package. Below code need to be added in NuGet package project.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

See https://gurunadhduvvuru.wordpress.com/2020/04/30/log4net-issues-when-moved-it-to-a-nuget-package/ for more details.

迷迭香的记忆 2024-09-24 05:09:47

确保 AssemblyInfo.cs 文件中应存在以下行代码。

[程序集: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

并在 Application_start() 方法中检查此行。

log4net.Config.XmlConfigurator.Configure();

Make sure the following line code should be there in AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

and also check for this line in Application_start() method.

log4net.Config.XmlConfigurator.Configure();
淡淡绿茶香 2024-09-24 05:09:47

您的配置文件似乎是正确的。然后,您必须将 Log4net 配置文件注册到应用程序。所以你可以使用下面的代码:

var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));

注册过程后,你可以调用下面的定义来调用记录器:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

log.Error("Sample log");

Your config file seems correct. Then, you have to register your Log4net config file to application. So you can use below code:

var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));

After registering process, you can call below definition to call logger:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

log.Error("Sample log");
草莓味的萝莉 2024-09-24 05:09:47

有几种使用 log4net 的方法。
我在寻找解决方案时发现它很有用。该解决方案如下所述: https://www.hemelix.com/log4net/

There are a few ways to use log4net.
I found it is useful while I was searching for a solution. The solution is described here: https://www.hemelix.com/log4net/

薯片软お妹 2024-09-24 05:09:46

你是否调用

log4net.Config.XmlConfigurator.Configure();

某个地方让 log4net 读取你的配置?例如在 Global.asax 中:

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

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

Do you call

log4net.Config.XmlConfigurator.Configure();

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();
}
鱼忆七猫命九 2024-09-24 05:09:46

使用此常见问题页面:Apache log4net 常见问题

大约 3/4下面它告诉您如何使用应用程序跟踪来启用 log4net 调试。这会告诉您问题出在哪里。

基础知识是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

您可以在标准输出中看到跟踪

Use this FAQ page: Apache log4net Frequently Asked Questions

About 3/4 of the way down it tells you how to enable log4net debugging by using application tracing. This will tell you where your issue is.

The basics are:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

And you see the trace in the standard output

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