log4net初始化
-
12-09-2019 - |
题
我看起来难重复,但必须要求以下,无论如何基本的,它可能看起来,让它清楚!
在一个新的控制台应用程序使用log4net版1.2.10.0在VS28KSP1 64位W7,我有下列代码:-
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 ]
或明确的初始化它在主要():
_log.Info("This will not go to the log");
XmlConfigurator.Configure();
_log.Info("Ran");
这就提出了下列问题:
- 我几乎可以肯定我已经看到了它工作的地方在一些版本的log4net不加的大会属性或呼叫中主要的。有人能向我保证我不想象那?
- 可以有人请我到这里的医生,它明确指出,这两种配置部分和初始化挂钩是必需的,希望有一个解释的时这种改变,如果它没有?
我可以很容易地想象一下为什么这个可能的政策--具有初始步骤的明确避免的惊喜等, 这只是我似乎回想一下,这并不总是被的情况下...(通常我已经配置在一个单独的文件,该文件通常需要configsections出的画面)
解决方案
根据 配置网页的手册:
该log4net构可用会级别特性而不是指定的编程方式。
XmlConfiguratorAttribute:的
log4net.Config.XmlConfiguratorAttribute
允许XmlConfigurator
要配置的使用以下性能:
- ConfigFile...
- ConfigFileExtension...
如果没有ConfigFile或ConfigFileExtension性是指定的,该应用程序配置文件(例如TestApp.exe。config)将被用作log4net配置文件。
例的使用情况:
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
我同意这是一个有点模棱两可,但是我解释的存在,例如使用意味着log4net不会使用。配置文件没有上述属性;而事实上,他们指出,你必须使用两个属性,但不说什么要离开出的属性,建议我的特性(或方案的呼叫)需要使用的应用程序。config在你想要的方式。
不隶属于 StackOverflow