配置log4net的使用XML文件 [英] configuring log4net with xml file

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

问题描述

您好我想log4net的配置为记录一切的控制台输出。我有nameed Log4Net.config一个配置文件

Hi I tried to configure log4net for logging everything to the console output. I have a config file nameed 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中

I added these lines to the AssemblyInfo.cs

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

但现在没有登录,有人能解释一下吗?

But now nothing is logged, can someone explain this?

在此先感谢

塞巴斯蒂安

推荐答案

当你有log4net的配置在一个单独的配置文件,你不应该包括配置和configSections元素。 log4net的应该是XML declartion后最高级别的元素。

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.

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

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