【问题标题】:Spring.Net logging with Log4Net not working使用 Log4Net 的 Spring.Net 日志记录不起作用
【发布时间】:2009-05-03 01:34:22
【问题描述】:

我无法让 Spring.Net 使用 Log4Net 进行日志记录。我对查看 Aspects 的日志记录特别感兴趣。我正在使用一个非常简单的日志配置,类似于 MovieFinder 示例应用程序:

...
<logger name="Spring">
  <level value="DEBUG" /> <!-- Have tried INFO as well, no different -->
  <appender-ref ref="SpringAppender"/>
</logger> 

<appender name="SpringAppender" type="log4net.Appender.RollingFileAppender">
  <file value="..\Log\Spring_Log.txt"/>
  <appendToFile value="true"/>
  <maximumFileSize value="100MB"/>
  <maxSizeRollBackups value="2"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level [%thread] %logger - %message%newline"/>
  </layout>
</appender>
...

文件“Spring_Log.txt”已创建,但没有记录任何内容(即空文件)。 Log4Net 目前正在正确记录 NHibernate 和我们的自定义应用程序日志记录。我正在使用 Spring.Net v1.2.0.20313 和 Log4Net v1.2.10.0。

有没有其他人遇到过他们能够解决的问题?非常感谢您的帮助,干杯。

【问题讨论】:

    标签: c# log4net spring.net


    【解决方案1】:

    正如 Erich 所说,您需要配置 Common.Logging。 您的 log4net 配置文件很好。这是我使用您的配置文件得到的结果:

    2009-05-02 19:08:40,890 DEBUG [10] Spring.Objects.Factory.Support.AbstractObjectDefinitionReader - Loading XML object definitions from config [C:\Documents and Settings\pczapla\My Documents\Visual Studio 2008\Projects\TimeLogger\TimeLogger\bin\Debug\TimeLogger.exe.config#spring/objects]
    2009-05-02 19:08:40,905 DEBUG [10] Spring.Objects.Factory.Support.AbstractObjectDefinitionReader - Using the following XmlReader implementation : System.Xml.XsdValidatingReader
    2009-05-02 19:08:40,921 DEBUG [10] Spring.Objects.Factory.Xml.DefaultObjectDefinitionDocumentReader - Loading object definitions.
    2009-05-02 19:08:40,921 DEBUG [10] Spring.Objects.Factory.Xml.ObjectDefinitionParserHelper - Loading object definitions...
    

    这里是如何配置 Common.Logging 的快速指南:

    在 lib 文件夹 (C:\Program Files\Spring.NET 1.2.0\lib\Net\2.0\) 中添加它们随 spring 一起提供的 Common.Logging 和 Common.Logging.Log4Net 程序集。 然后将以下配置添加到您的app.config

    <configuration>
        </configSections>
            ...
            <sectionGroup name="common">
                <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
            </sectionGroup>
        </configSections>
        ...
        <common>
            <logging>
                <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
                    <!-- Common Logging assumes that log4net is initialized -->
                    <arg key="configType" value="EXTERNAL"/>
                    <!-- Or it can configure log4net for you
                    <arg key="configType" value="FILE-WATCH" />
                    <arg key="configFile" value="path\to\your\log4net.config" />
                    -->
                </factoryAdapter>
            </logging>
        </common>
    </configuration>
    

    就是这样。现在您应该从 spring 中获取调试消息。

    【讨论】:

      【解决方案2】:

      Spring.NET 使用 Common.Logging。您是否将 Common.Logging 配置为登录到 log4net?有关文档,请参阅http://netcommon.sourceforge.net/documentation.html

      hth, 埃里希

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        • 2018-08-24
        • 2011-12-27
        • 2013-04-27
        相关资源
        最近更新 更多