log4net 不会从 app.config 读取
我有两个为 log4net 配置相同的项目。一个项目记录良好;然而,另一个根本不记录。
不进行日志记录的项目中的 Logger
返回 IsFatalEnabled = false
、IsErrorEnabled = false
、IsWarnEnabled = false
、 IsInforEnabled = false
和 IsDebugEnabled = false
。
我已从一个项目复制并粘贴到另一个项目,完全替换该文件并尝试删除所有空白。
是什么导致一个项目无法正确从 app.config 读取正确的级别?
app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date: %-5level – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
Program.cs
using log4net;
class Program
{
private static readonly ILog Log = LogManager.GetLogger("SO");
static void Main(string[] args)
{
Log.Info("SO starting");
}
}
I have two projects configured identically for log4net. One project logs fine; however, the other does not log at all.
The Logger
in the project that is not logging returns IsFatalEnabled = false
, IsErrorEnabled = false
, IsWarnEnabled = false
, IsInforEnabled = false
and IsDebugEnabled = false
.
I've copied and pasted from one project to the other, replaced the file completely and tried removing all whitespace.
What could be causing the one project not to properly be reading the correct levels from the app.config?
app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date: %-5level – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
Program.cs
using log4net;
class Program
{
private static readonly ILog Log = LogManager.GetLogger("SO");
static void Main(string[] args)
{
Log.Info("SO starting");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
app.config 文件似乎未配置为由 log4net 监视。
我将以下行添加到
AssemblyInfo.cs
中,并且日志记录现已启用:奇怪,因为我没有将此行添加到正在运行的项目中。
编辑:
看起来正在运行的项目毕竟有这条线。我一定是忘记了。
It seems that the app.config file was not configured to be watched by log4net.
I added the following line to
AssemblyInfo.cs
and logging is now enabled:Weird, since I didn't add this line to the the project that was working.
EDIT:
Looks like the project that was working did have the line after all. I must have forgotten.
我的解决方案是在程序的启动代码中使用
XmlConfigurator.Configure()
。但是,我的情况有所不同,因为我使用 Autofac.log4net 模块在 Autofac 容器中注册ILog
。在本例中,使用XmlConfiguratorAttribute
(将Watch
设置为 true 或 false)使记录器保持未配置状态。The solution for me was to use
XmlConfigurator.Configure()
in the program's startup code. However, my situation differed in that I was using the Autofac.log4net module to register theILog
in an Autofac container. In this instance, usingXmlConfiguratorAttribute
(withWatch
set to either true or false) left the logger unconfigured.