log4net初始化
我努力寻找重复项,但必须问以下问题,无论它看起来多么基本,一劳永逸地弄清楚!
在 64 位 W7 上的 VS28KSP1 上使用 log4net 版本 1.2.10.0 的新控制台应用程序中,我有以下代码:-
using log4net;
using log4net.Config;
namespace ConsoleApplication1
{
class Program
{
static readonly ILog _log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
_log.Info("Ran");
}
}
}
在我的 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="Program.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%username] %date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
这不会写任何内容,除非我添加属性:
[ assembly:XmlConfigurator ]
或者在 Main() 中显式初始化它:
_log.Info("This will not go to the log");
XmlConfigurator.Configure();
_log.Info("Ran");
这会引发以下问题:
- 我几乎可以肯定我已经看到它在某些版本的 log4net 上的某个地方工作,而无需添加程序集属性或在 Main 中进行调用。 有人可以向我保证这不是我的想象吗?
- 有人可以指出我在文档中明确指出配置部分和初始化挂钩都是必需的 - 希望能解释一下何时更改(如果确实如此)吗?
我可以很容易地想象为什么这可能是政策——明确初始化步骤以避免意外等,只是我似乎记得情况并非总是如此......(通常我将配置放在一个单独的文件中,通常将配置部分从图片中删除)
I've looked hard for duplicates but have to ask the following, no matter how basic it may seem, to get it clear once and for all!
In a fresh Console app using log4net version 1.2.10.0 on VS28KSP1 on 64 bit W7, I have the following code:-
using log4net;
using log4net.Config;
namespace ConsoleApplication1
{
class Program
{
static readonly ILog _log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
_log.Info("Ran");
}
}
}
In my app.config
, I have:
<?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="Program.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%username] %date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
This doesnt write anything, unless I either add an attribute:
[ assembly:XmlConfigurator ]
Or explicitly initialise it in Main():
_log.Info("This will not go to the log");
XmlConfigurator.Configure();
_log.Info("Ran");
This raises the following questions:
- I'm almost certain I've seen it working somewhere on some version of log4net without the addition of the assembly attribute or call in Main. Can someone assure me I'm not imagining that?
- Can someone please point me to where in the doc it explicitly states that both the config section and the initialisation hook are required - hopefully with an explanation of when this changed, if it did?
I can easily imagine why this might be the policy -- having the initialisation step explicit to avoid surprises etc., it's just that I seem to recall this not always being the case... (And normally I have the config in a separate file, which generally takes configsections out of the picture)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据手册中的配置页面:
我同意这有点含糊,但我将示例用法的存在解释为意味着 log4net 不会使用没有上述属性的 .config 文件; 事实上,他们指出您必须使用这两个属性之一,但不要说完全省略该属性,这表明在我看来,需要该属性(或编程调用)才能在你想要的方式。
According to the configuration page in the manual:
I agree that it's a bit ambiguous, but I interpret the existence of the example usage to mean that log4net will not use the .config file without the above attribute; and the fact that they point out that you have to use one of the two properties, but do not say anything about leaving out the attribute altogether, suggests to me that the attribute (or programmatic call) is required to use app.config in the way you want.