我看很难复制,但要问下,无论多么基本的,它可能看起来,得到明确一劳永逸!
在使用上VS28KSP1 log4net的版本1.2.10.0一个新的控制台应用程序在64位的W7,我有以下的code: -
使用log4net的; 使用log4net.Config; 命名空间ConsoleApplication1 { 类节目 { 静态只读的ILog _log = LogManager.GetLogger(typeof运算(计划)); 静态无效的主要(字串[] args) { _log.Info(然); } } }在我的的app.config ,我有:
< XML版本=1.0编码=UTF-8&GT?; <结构> < configSections> <节名称=log4net的TYPE =log4net.Config.Log4NetConfigurationSectionHandler,log4net的/> < / configSections> < log4net的> <附加目的地名称=RollingFileAppender进行式=log4net.Appender.RollingFileAppender> <文件值=Program.log/> < lockingModel TYPE =log4net.Appender.FileAppender + MinimalLock/> < appendToFile值=真/> < rollingStyle值=大小/> < maxSizeRollBackups值=10/> < maximumFileSize值=1 MB/> < staticLogFileName值=真/> <布局类型=log4net.Layout.PatternLayout> < conversionPattern值=[%的用户名]%日[%线程]%-5level%记录[%属性{NDC}] - %消息%换行/> < /布局> < /附加器> <根> <电平值=DEBUG/> <附加目的地-REF REF =RollingFileAppender进行/> < /根> < / log4net的> < /结构>
这并不写任何东西,除非我要么添加一个属性:
[总成:XmlConfigurator]
或显式地初始化它在main():
_log.Info(这不会去日志); XmlConfigurator.Configure(); _log.Info(然);这引起了以下问题:
我可以很容易想象为什么这可能是政策 - 具有初始化步骤明确,以避免意外等,这只是我似乎记得这不一定是这样...(而且通常我在配置一个单独的文件,这通常需要configsections出来的画面)
解决方案据the在手动配置页:
可以使用汇编级配置log4net的配置属性,而不是编程方式指定。
XmlConfiguratorAttribute:在 log4net.Config.XmlConfiguratorAttribute 允许 XmlConfigurator 使用以下属性来配置:
- ConfigFile实现...
- ConfigFileExtension ...
如果既不ConfigFile实现或ConfigFileExtension属性的指定时,应用程序的配置文件(例如TestApp.exe.config)将被用作log4net的配置文件。
实例:
使用config文件 //配置log4net的 [总成:log4net.Config.XmlConfigurator(表=真) //这将导致log4net的寻找一个配置文件 //在应用基础名为TestApp.exe.config //目录(即包含TestApp.exe的目录) //配置文件将被观察的变化。
我同意这是一个有点暧昧,但我除preT的用法示例的存在意味着没有log4net的上述属性,不会使用config文件;并且,他们指出,你必须使用这两个属性之一,但不要说的完全离开了该属性什么的事实,建议对我的属性(或程序调用)需要使用的app.config在你想要的方式。
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 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)
解决方案According to the configuration page in the manual:
The log4net configuration can be configured using assembly-level attributes rather than specified programmatically.
XmlConfiguratorAttribute: The log4net.Config.XmlConfiguratorAttribute Allows the XmlConfigurator to be configured using the following properties:
- ConfigFile ...
- ConfigFileExtension ...
If neither of the ConfigFile or ConfigFileExtension properties are specified, the application configuration file (e.g. TestApp.exe.config) will be used as the log4net configuration file.
Example usage:
// 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.
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.
更多推荐
log4net的初始化
发布评论