log4net 初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:08:05  来源:igfitidea点击:

log4net initialisation

.netlog4netinitializationlog4net-configurationxmlconfigurator

提问by Ruben Bartelink

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:-

在 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");
        }
    }
}

In my app.config, I have:

在我的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>

This doesnt write anything, unless I either add an attribute:

这不会写任何东西,除非我添加一个属性:

[ assembly:XmlConfigurator ]

Or explicitly initialise it in Main():

或者在 Main() 中显式初始化它:

_log.Info("This will not go to the log");
XmlConfigurator.Configure();
_log.Info("Ran");

This raises the following questions:

这就提出了以下问题:

  1. 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?
  2. 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?
  1. 我几乎可以肯定我已经看到它在某些版本的 log4net 上运行,而没有添加程序集属性或调用 Main。有人可以向我保证我不是在想象吗?
  2. 有人可以指出我在文档中明确指出需要配置部分和初始化钩子的地方 - 希望能解释一下何时更改,如果更改了吗?

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)

我可以很容易地想象为什么这可能是策略 - 明确初始化步骤以避免意外等,只是我似乎记得情况并非总是如此......(通常我将配置放在单独的文件中,这通常会从图片中删除配置部分)

回答by Mark Rushakoff

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.XmlConfiguratorAttributeAllows the XmlConfiguratorto 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:

log4net 配置可以使用程序集级别的属性进行配置,而不是以编程方式指定。

XmlConfiguratorAttribute:log4net.Config.XmlConfiguratorAttribute允许XmlConfigurator使用以下属性进行配置:

  • 配置文件...
  • 配置文件扩展...

如果 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.

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 不会使用没有上述属性的 .config 文件;并且他们指出您必须使用这两个属性之一,但没有说完全省略该属性的事实,向我表明该属性(或编程调用)需要在应用程序中使用 app.config你想要的方式。