使用 xml 文件配置 log4net [英] Configuring log4net with xml file

查看:38
本文介绍了使用 xml 文件配置 log4net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试配置 log4net 以将所有内容记录到控制台输出.我有一个名为 Log4Net.config

I tried to configure log4net for logging everything to the console output. I have a config file named Log4Net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

我有我的主课(只是一个测试用例)

and I have my main class (just a testing case)

namespace TestLog4Net {
    class Program {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args) {
            log.Info("Info");
            log.Error("Error");
            Console.ReadKey();
        }
    }
}

我将这些行添加到 AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(
ConfigFile = "Log4Net.config", Watch = true)]

但是现在什么都没有记录,有人可以解释一下吗?

But now nothing is logged, can someone explain this?

推荐答案

当您在单独的配置文件中有 log4net 配置时,您不应包含配置和 configSections 元素.log4net 应该是 xml 声明之后的顶级元素.

When you have log4net config in a separate config file you should not include the configuration and configSections elements. log4net should be the top level element after the xml declartion.

这篇关于使用 xml 文件配置 log4net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆