1

我无法让 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。

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

4

2 回答 2

3

正如 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 的快速指南:

添加 Common.Logging 和 Common.Logging.Log4Net 程序集,它们在 lib 文件夹C:\Program Files\Spring.NET 1.2.0\lib\Net\2.0\(然后将以下配置添加到您的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 中获取调试消息。

于 2009-05-02T17:34:22.253 回答
1

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

hth,埃里希

于 2009-04-27T15:24:04.200 回答