Log4net不会将日志写入日志文件
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
另外,请确保为 [log4net].config 选择“始终复制”选项
Also, Make sure the "Copy always" option is selected for [log4net].config
正如@AndreasPaulsson 所建议的,我们需要配置它。我正在
AssemblyInfo
文件中进行配置。我在此处指定配置文件名
。As @AndreasPaulsson suggested, we need to configure it. I am doing the configuration in
AssemblyInfo
file. I specify theconfiguration file name
here.确保运行站点的进程(帐户)具有写入输出目录的权限。
在 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.
插入:
在 AssemblyInfo.cs 文件末尾
Insert:
at the end of AssemblyInfo.cs file
就我而言,我必须授予日志文件的 IIS_IUSRS 读\写权限。
In my case I had to give the
IIS_IUSRS
Read\write permission to the log file.对我来说,我移动了日志文件的位置,只有当我将文件名更改为其他名称时,它才会再次启动。
似乎如果已经存在同名的日志文件,则不会发生任何情况。
之后,我重命名旧文件并将配置中的日志文件名再次更改回原来的名称。
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.
就我而言,由于项目名称中存在空格,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.
对我来说,我必须将 Logger 移至 Nuget 包。需要在 NuGet 包项目中添加以下代码。
请参阅 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.
See https://gurunadhduvvuru.wordpress.com/2020/04/30/log4net-issues-when-moved-it-to-a-nuget-package/ for more details.
确保 AssemblyInfo.cs 文件中应存在以下行代码。
[程序集: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
并在 Application_start() 方法中检查此行。
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 配置文件注册到应用程序。所以你可以使用下面的代码:
注册过程后,你可以调用下面的定义来调用记录器:
Your config file seems correct. Then, you have to register your Log4net config file to application. So you can use below code:
After registering process, you can call below definition to call logger:
有几种使用 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/
你是否调用
某个地方让 log4net 读取你的配置?例如在 Global.asax 中:
Do you call
somewhere to make log4net read your configuration? E.g. in Global.asax:
使用此常见问题页面:Apache log4net 常见问题
大约 3/4下面它告诉您如何使用应用程序跟踪来启用 log4net 调试。这会告诉您问题出在哪里。
基础知识是:
您可以在标准输出中看到跟踪
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:
And you see the trace in the standard output