Configuring Log4NetLoggerFactoryAdapter Programmatically

你说的曾经没有我的故事 提交于 2019-12-23 23:26:21

问题


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).


回答1:


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();



回答2:


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>


来源:https://stackoverflow.com/questions/9011494/configuring-log4netloggerfactoryadapter-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!