以编程方式配置Log4NetLoggerFactoryAdapter [英] Configuring Log4NetLoggerFactoryAdapter Programmatically

查看:134
本文介绍了以编程方式配置Log4NetLoggerFactoryAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

带有以下文件:

<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运行器中看到日志输出(文本输出).

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

推荐答案

这就是我最终要做的事情-满足了我希望在NUnit运行器控制台中查看输出的目标,并且我最终添加了一个滚动文件记录器好吧.

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.

在测试课中:

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

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

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>

这篇关于以编程方式配置Log4NetLoggerFactoryAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆