Configuring Log4NetLoggerFactoryAdapter Programmat

2019-07-24 18:57发布

I am using NUnit to test a project and I'd like to configure my tests to setup Common.Logging programmatically to use Log4Net. Here's what I've tried:

        NameValueCollection config = new NameValueCollection();
        //config.Add("configType", "EXTERNAL");

        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DevelopMENTALMadness.Data.Sql.Tests.loggerconfig.xml");
        XmlConfigurator.Configure(stream);

        LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);

With the following file:

<log4net>
<appender name="A1" type="log4net.Appender.ConsoleAppender">

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%thread] %-4timestamp %-5level %logger %ndc - %message%newline" />
    </layout>
</appender>

<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
    <level value="DEBUG" />
    <appender-ref ref="A1" />
</root>

And

        NameValueCollection config = new NameValueCollection();
        //config.Add("configType", "EXTERNAL");

        var x = new ConsoleAppender { Layout = new PatternLayout("[%thread] %-4timestamp %-5level %logger %ndc - %message%newline") };
        BasicConfigurator.Configure(x);

        LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);

But either it doesn't use the pattern I specify or if I uncomment the "configType" line it displays nothing at all. I'm just trying to select the layout I want so when I'm debugging my tests I can see the log output in the NUnit runner (Text Output).

2条回答
【Aperson】
2楼-- · 2019-07-24 19:50

Just a heads up, Common.Logging comes with a bunch of default log adapters - one of which is a console appender.

Using this removes the need for maintaining logger configuration in test assemblies.

Configuration is a one liner in your test fixture:

Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter();
查看更多
The star\"
3楼-- · 2019-07-24 19:51

So here's what I ended up doing - it meets my goal of wanting to see the output in NUnit runner console, plus I ended up adding a rolling file logger as well.

In the test class:

[TestFixtureSetUp]
public void Init()
{
    BasicConfigurator.Configure(new ConsoleAppender());
}

Then I have an App.config file (copy always):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
                <arg key="configType" value="INLINE"/>
            </factoryAdapter>
        </logging>
    </common>

    <log4net debug="false">
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="./Tests.log" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="50MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
            </layout>
            <threshold value="DEBUG" />
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
            </layout>
        </appender>
        <root>
            <priority value="ALL"/>
            <appender-ref ref="RollingFileAppender"/>
            <appender-ref ref="ConsoleAppender" />
        </root>
    </log4net>
</configuration>
查看更多
登录 后发表回答