以编程方式配置Log4NetLoggerFactoryAdapter

时间:2012-01-25 22:41:47

标签: .net log4net log4net-configuration

我正在使用NUnit来测试项目,我想配置我的测试以编程方式设置Common.Logging以使用Log4Net。这是我尝试过的:

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

使用以下文件:

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

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

但要么它不使用我指定的模式,要么我取消注释“configType”行它根本不显示任何内容。我只是想选择我想要的布局,所以当我调试我的测试时,我可以看到NUnit运行器中的日志输出(文本输出)。

2 个答案:

答案 0 :(得分:2)

简单来说,Common.Logging附带了一堆默认日志适配器 - 其中一个是控制台appender。

使用此功能无需在测试程序集中维护记录器配置。

配置是测试夹具中的一个衬垫:

Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter();

答案 1 :(得分:0)

所以这就是我最终做的事情 - 它符合我想要在NUnit跑步控制台中查看输出的目标,而且我最终还添加了一个滚动文件记录器。

在测试课程中:

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

然后我有一个App.config文件(总是复制):

<?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>