6

log4net を使用して Windows イベント ビューアーにログインしたいと考えています。
コンソール アプリケーション (.NET Framework 4) を作成し、参照 log4net.dll を追加し、App.config に次のコードを追加しました。

<configuration>
 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
 </configSections>

<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
  </layout>
</appender>
<root>
  <level value="ALL"/>
  <appender-ref ref="EventLogAppender"/>
</root>
</log4net>

<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>

そして、次のコードを入れます:

class Program
{
    static void Main(string[] args)
    {
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Error("test error", new Exception("error's exception", new Exception("error's innerexception")));

        Console.Read();
    }
}

ログに記録されず、何も起こりません。なぜですか?

ありがとう

4

3 に答える 3

10

You need to call configure.

Change:

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

To

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

When you specify ConfigFile = "App.config" its going to look for App.config but your filename would be [FileName].Config.

于 2011-06-29T15:35:25.887 に答える
5

初期化するには、log4net ライブラリからXmlConfigurator.Configureを呼び出す必要があります。(下記参照)

class Program
{
    static void Main(string[] args)
    {
        // you need this
        XmlConfigurator.Configure();
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Error("test error", new Exception("error's exception", new Exception("error's innerexception")));

        Console.Read();
    }
}
于 2011-06-29T15:36:47.760 に答える