如何使用log4net配置Spring.NET中的日志记录?

时间:2011-11-17 18:52:21

标签: log4net spring.net log4net-configuration

我想从Spring.NET Aspect库使用log4方面和log4Net一起使用。 我发现this article如何将log4Net与Common Logging API一起使用。

我的测试应用程序是控制台,基于.NET 4.0客户端配置文件。

第一次尝试

所以我在我的项目中引用这些库:

  • Spring.Core版本1.3.2.40943,运行时版本v4.0.30319
  • Spring.AOP版本1.3.2.40943,运行时版本v4.0.30319
  • Common.Logging版本:1.2.0.0,运行时v1.0.3705

上面的程序集来自Program Files \ Spring.NET 1.3.2 \ bin \ net \ 4.0

  • log4net version:1.2.10.0,runtime v2.0.50727

我无法在 Program Files \ Spring.NET 1.3.2 \ bin中找到程序集Common.Logging.Log4Net.dll \ net \ 4.0 所以我下载这个程序集from SourceForge

  • Common.Logging.Log4Net version 2.0.0.0 runtime v2.0.50727

我在app.config中配置了logger:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
      <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
          <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
      </sectionGroup>
      <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>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </root>

    <logger name="myLogger">
      <level value="DEBUG" />
    </logger>
  </log4net>

  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
    </objects>
  </spring>
</configuration>

并尝试了这个:

ILog log = LogManager.GetLogger("myLogger");
log.Error("hello world");

我收到了这个运行时错误:

  

{&#34;无法从配置部分配置Common.Logging   &#39;公共/记录&#39;&#34;}

内部异常:

  

{&#34;创建配置节处理程序时出错   common / logging:无法创建类型   &#39; Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,   Common.Logging.Log4Net&#39; (E:\ C#   项目\研究\ SPRING.NET \ SpringNet.Aspects \ LoggingWithLog4Net \ BIN \调试\ LoggingWithLog4Net.vshost.exe.Config   第18行&#34;}

堆栈跟踪:

  

在System.Configuration.BaseConfigurationRecord.EvaluateOne(String []   keys,SectionInput输入,Boolean isTrusted,FactoryRecord   factoryRecord,SectionRecord sectionRecord,Object parentResult)at   System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord   factoryRecord,SectionRecord sectionRecord,Object parentResult,   Boolean getLkg,Boolean getRuntimeObject,Object&amp;结果,对象&amp;   resultRuntimeObject)at   System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串   configKey,Boolean getLkg,Boolean checkPermission,Boolean   getRuntimeObject,Boolean requestIsHere,Object&amp;结果,对象&amp;   resultRuntimeObject)at   System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串   configKey,Boolean getLkg,Boolean checkPermission,Boolean   getRuntimeObject,Boolean requestIsHere,Object&amp;结果,对象&amp;   resultRuntimeObject)at   System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串   configKey,Boolean getLkg,Boolean checkPermission,Boolean   getRuntimeObject,Boolean requestIsHere,Object&amp;结果,对象&amp;   resultRuntimeObject)at   System.Configuration.BaseConfigurationRecord.GetSection(字符串   configKey)   System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(字符串   sectionName)at   System.Configuration.ConfigurationManager.GetSection(字符串   sectionName)at   System.Configuration.ConfigurationSettings.GetConfig(字符串   sectionName)at   Common.Logging.ConfigurationReader.GetSection(String sectionName)
  在Common.Logging.LogManager.BuildLoggerFactoryAdapter()

第二次尝试

我认为问题必须是程序集Common.Logging.Log4Net version 2.0.0.0 runtime v2.0.50727 的版本。因为我使用了Common.Logging 版本:1.2.0.0,运行时v1.0.3705

所以我将Common.Logging版本从1.2.0.0更改为2.0.0.0。我使用了来自SourceForge的Common.Logging。

再次测试。我收到了这个错误:

  

无法创建类型   &#39; Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,   Common.Logging.Log4Net&#39;

内部异常:

  

{&#34;无法加载文件或程序集&#39; Common.Logging.Log4Net&#39;或者其中之一   它的依赖关系。系统找不到该文件   指定&#34;:&#34; Common.Logging.Log4Net&#34;}

堆栈跟踪:

  

at System.RuntimeTypeHandle.GetTypeByName(String name,Boolean   throwOnError,Boolean ignoreCase,Boolean reflectionOnly,   StackCrawlMarkHandle stackMark,Boolean loadTypeFromPartialName,   ObjectHandleOnStack类型)at   System.RuntimeTypeHandle.GetTypeByName(String name,Boolean   throwOnError,Boolean ignoreCase,Boolean reflectionOnly,   StackCrawlMark&安培; stackMark,Boolean loadTypeFromPartialName)at   System.RuntimeType.GetType(String typeName,Boolean throwOnError,   Boolean ignoreCase,Boolean reflectionOnly,StackCrawlMark&amp; stackMark)   在System.Type.GetType(String typeName,Boolean throwOnError,Boolean   ignoreCase)at   Common.Logging.ConfigurationSectionHandler.ReadConfiguration(XmlNode的   部分)   C:\ CCNET \ netcommon \树干\模块\ Common.Logging \ SRC \共同\ Common.Logging \记录\ ConfigurationSectionHandler.cs:线   130

第3次和最后一次尝试

最后我在spring示例中找到了程序集Common.Logging.Log4Net 1.2.0.2版,所以我用它。

  • Common.Logging.Log4Net 1.2.0.2版
  • Common.Logging版本1.2.0.0
  • log4Net 1.2.10.0

经过测试并再次出现错误:

  

{&#34;无法从配置部分配置Common.Logging   &#39;公共/记录&#39;&#34;}

内部异常:

  

{&#34;创建配置节处理程序时出错   common / logging:无法创建类型   &#39; Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,   Common.Logging.Log4Net&#39; (E:\ C#   项目\研究\ SPRING.NET \ SpringNet.Aspects \ LoggingWithLog4Net \ BIN \调试\ LoggingWithLog4Net.vshost.exe.Config   第18行&#34;}

堆栈跟踪:

  

{&#34;无法创建类型   &#39; Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,   Common.Logging.Log4Net&#39;&#34;}

我真的很困惑我的坏事?程序集版本有些问题吗?

3 个答案:

答案 0 :(得分:5)

对于Spring.NET 1.3.2,您应该使用:

  • Common.Logging 2.0.0
  • Common.Logging.Log4Net 2.0.0
  • log4net 1.2.10

确保将最后两个log4net dll复制到输出目录。由于您的代码不直接引用它,因此Visual Studio可能无法复制它们。

答案 1 :(得分:1)

此外,而不是

<resource uri="config://springs/objects"/>

尝试

<resource uri="config://spring/objects"/>

在你的配置文件中,sectionGroup名称是&#34; spring&#34;,NOT&#34; spring&#34;。我从你的身上复制了我的工作并遇到了同样的问题。终于找到了原因。

希望有所帮助!

答案 2 :(得分:1)

另一种可能性是:

  • Common.Logging 2.1.1
  • Common.Logging.Log4Net 2.1.1
  • log4net 1.2.10

我建议跳过Common.Logging 2.1.0,因为它在读取完全有效的app.config部分时会抛出此异常:

“无法将类型为'System.Configuration.DefaultSection'的对象强制转换为'System.Configuration.AppSettingsSection'。”

安装2.1.1修复了问题。

相关问题