Log4Net 未记录(可能未初始化)
第一次玩 Log4Net,我遇到了麻烦。我已经遵循了各种教程,但我无法让它注销任何内容。让我向您展示我的代码,希望您能告诉我我做错了什么。
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="CurrentLog.txt"/>
<staticLogFileName value="true"/>
<appendToFile value="true"/>
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd"/>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd MMM yyy HH:mm:ss} %level %message. %newline %exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
Presenter.cs
在课程的顶部我有这个:
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
然后我稍后尝试使用日志变量类:
bool isDebugEnabled = log.IsDebugEnabled;
log.Debug("Failed to save", e);
每当我检查 isDebugEnabled
变量时,它都是 false,如果我检查日志变量,则所有其他 isBlahEnabled
也是如此。
我怀疑我没有正确连接我的 app.config 文件,因为这是我第一次尝试使用它。我通过右键单击解决方案资源管理器中的项目,添加一个新项目,选择应用程序配置文件并将其命名为 app.config 来创建。
First time playing with Log4Net and I'm running into trouble. I've followed various tutorials but I've been unable to get it to log anything out. Let me show you my code and hopefully you can tell me what I'm doing wrong.
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="CurrentLog.txt"/>
<staticLogFileName value="true"/>
<appendToFile value="true"/>
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd"/>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd MMM yyy HH:mm:ss} %level %message. %newline %exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
Presenter.cs
At the top of the class I have this:
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Then I try to use the log variable later in the class:
bool isDebugEnabled = log.IsDebugEnabled;
log.Debug("Failed to save", e);
Whenever I inspect the isDebugEnabled
variable, it is false, as are all of the other isBlahEnabled
if I inspect the log variable.
My suspicion is that I have not hooked up my app.config file correctly because this is the first time I have tried to use one. I created by right clicking on the project in solution explorer, adding a new item, choosing Application Configuration File and naming it app.config.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
app.config
另外,请确保
app.config
上的构建操作设置为 None 并复制到输出目录设置为“如果较新则复制”。您可以在文件属性中设置这些设置。Program.cs
This one works for me:
app.config
Also be sure the build action on
app.config
is set to None and Copy to output dir is set to "Copy if newer". You can set these settings in the file properties.Program.cs
只是补充我的 2 美分:我遇到了同样的问题,但我在单独的
log4net.config
中使用上述配置(因此我在所有应用程序上都有相同的配置),但我只是忘记设置构建上的文件属性“较新时复制”(或类似的措辞,有德语版本)。Just to add my 2 cents: I had the same Problem, but I use the above config in separate
log4net.config
(so I have on all apps the same config), but I simply forgot to set the file property on build 'Copy when newer' (or similar wording, have German Version).只是添加我的 2p,以防其他人寻找此问题的解决方案:
在我的例子中,除了 Log4net 之外,我还使用 NServicebus.Logging 包,并且该包包含与 Log4net 中名称完全相同的类型,仅在命名空间上有所不同。
修复“using”语句或完全限定类型名称解决了这个问题,但很难发现问题。
Just to add my 2p in case this helps anyone else searching for a resolution to this problem:
In my case I was using the NServicebus.Logging package in addition to Log4net, and this package contains types with the exact same names as those in Log4net, differing only in the namespace.
Fixing the 'using' statements or fully-qualifying the type names resolved it, but it was very hard to spot the problem.