C# log4net in UnitTests

Um log4net in Unit-Tests zu verwenden, muss die Konfiguration aufgerufen werden. Hierzu kann man eine StartUp-Klasse anlegen:

[TestClass]
public static class Startup
{
    [AssemblyInitialize]
    public static void Configure(TestContext tc)
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}

Die Konfiguration von log4net kann in der app.config erfolgen:

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

<log4net>
<root>
  <level value="ALL" />
  <appender-ref ref="console" />
  <appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
  </layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
  <file value="c:\\logs\\app.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="5" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline"  />
  </layout>
</appender>
</log4net>

Werbung



comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum