如何使用log4net在Spring.NET中配置日志记录? [英] How to configure logging in Spring.NET using log4net?

查看:84
本文介绍了如何使用log4net在Spring.NET中配置日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Spring.NET方面库Logging方面与log4Net一起使用. 我发现了本文结合使用Log4Net和Common Logging API.

I would like use from Spring.NET Aspect library Logging aspect together with log4Net. I found this article how to use log4Net with Common Logging API.

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

My test application is console and based on .NET 4.0 Client profile.

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

So I refer in my project these libraries:

  • Spring.Core版本1.3.2.40943,运行时v4.0.30319
  • Spring.AOP版本1.3.2.40943,运行时v4.0.30319
  • 常用日志记录版本:1.2.0.0,运行时v1.0.3705

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

Assemblies above I have from Program Files\Spring.NET 1.3.2\bin\net\4.0

  • log4net版本:1.2.10.0,运行时v2.0.50727

我在 Program Files \ Spring.NET 1.3.2 \ bin中找不到程序集Common.Logging.Log4Net.dll \ net \ 4.0 ,所以我从SourceForge下载该程序集 :

I couldn’t find assembly Common.Logging.Log4Net.dll in Program Files\Spring.NET 1.3.2\bin \net\4.0 so I download this assembly from SourceForge:

  • Common.Logging.Log4Net版本2.0.0.0运行时v2.0.50727

我在app.config中配置了记录器:

I configured logger in app.config:

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

我收到此运行时错误:

{无法配置Common.Logging from配置部分 'common/logging'."}

{"Could not configure Common.Logging from configuration section 'common/logging'."}

内部异常:

{在为以下内容创建配置节处理程序时发生错误 普通/日志:无法创建类型 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'(E:\ C# 项目\研究\ SPRING.NET \ SpringNet.Aspects \ LoggingWithLog4Net \ bin \ Debug \ LoggingWithLog4Net.vshost.exe.Config 第18行)}

{"An error occurred creating the configuration section handler for common/logging: Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net' (E:\C# PROJECTS\STUDY\SPRING.NET\SpringNet.Aspects\LoggingWithLog4Net\bin\Debug\LoggingWithLog4Net.vshost.exe.Config line 18)"}

StackTrace:

StackTrace:

at System.Configuration.BaseConfigurationRecord.EvaluateOne(String [] 键,SectionInput输入,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult)位于 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult, 布尔值getLkg,布尔值getRuntimeObject,对象&结果,对象与对象 resultRuntimeObject) System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,布尔值getLkg,布尔值checkPermission,布尔值 getRuntimeObject,布尔型请求IsHere,Object&结果,对象与对象 resultRuntimeObject) System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,布尔值getLkg,布尔值checkPermission,布尔值 getRuntimeObject,布尔型请求IsHere,Object&结果,对象与对象 resultRuntimeObject) System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,布尔值getLkg,布尔值checkPermission,布尔值 getRuntimeObject,布尔型请求IsHere,Object&结果,对象与对象 resultRuntimeObject) System.Configuration.BaseConfigurationRecord.GetSection(String configKey) System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)位于 System.Configuration.ConfigurationManager.GetSection(String sectionName)位于 System.Configuration.ConfigurationSettings.GetConfig(字符串 sectionName)位于 Common.Logging.ConfigurationReader.GetSection(String sectionName)
在Common.Logging.LogManager.BuildLoggerFactoryAdapter()

at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at System.Configuration.ConfigurationSettings.GetConfig(String sectionName) at Common.Logging.ConfigurationReader.GetSection(String sectionName)
at Common.Logging.LogManager.BuildLoggerFactoryAdapter()

第二次尝试

我认为问题必须出在程序集Common.Logging.Log4Net 版本2.0.0.0运行时v2.0.50727 的版本中.因为我使用的是Common.Logging 版本:1.2.0.0,所以运行时v1.0.3705 .

2nd Attempt

I think problem must be in version of assembly Common.Logging.Log4Net version 2.0.0.0 runtime v2.0.50727. Because I used Common.Logging version: 1.2.0.0, runtime v1.0.3705.

因此,我将Common.Logging版本从1.2.0.0更改为2.0.0.0.我从 SourceForge 使用了Common.Logging.

So I changed Common.Logging version from 1.2.0.0 to 2.0.0.0. I used Common.Logging from SourceForge.

并再次测试.我收到此错误:

And tested again. I got this error:

无法创建类型 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'

Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'

内部异常:

{无法加载文件或程序集'Common.Logging.Log4Net'或以下之一 它的依赖关系.系统找不到文件 指定.:" Common.Logging.Log4Net}

{"Could not load file or assembly 'Common.Logging.Log4Net' or one of its dependencies. The system cannot find the file specified.":"Common.Logging.Log4Net"}

StackTrace:

StackTrace:

在System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔值 throwOnError,布尔ignoreCase,布尔反射Only, StackCrawlMarkHandle stackMark,布尔值loadTypeFromPartialName, ObjectHandleOnStack类型),位于 System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔值 throwOnError,布尔ignoreCase,布尔反射Only, StackCrawlMark& stackMark,布尔值loadTypeFromPartialName),位于 System.RuntimeType.GetType(String typeName,布尔throwOnError, 布尔值ignoreCase,布尔值反射专用,StackCrawlMark& stackMark) 在System.Type.GetType(String typeName,布尔throwOnError,布尔 (ignoreCase)在 Common.Logging.ConfigurationSectionHandler.ReadConfiguration(XmlNode 部分) c:\ CCNet \ netcommon \ trunk \ modules \ Common.Logging \ src \ Common \ Common.Logging \ Logging \ ConfigurationSectionHandler.cs:line 130

at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) 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& stackMark) at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) at Common.Logging.ConfigurationSectionHandler.ReadConfiguration(XmlNode section) in c:\CCNet\netcommon\trunk\modules\Common.Logging\src\Common\Common.Logging\Logging\ConfigurationSectionHandler.cs:line 130

第三次也是最后一次尝试

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

3rd and last attempt

Finally I found assembly Common.Logging.Log4Net version 1.2.0.2 in spring examples so I used it.

  • Common.Logging.Log4Net版本1.2.0.2
  • 常用日志记录版本1.2.0.0
  • log4Net 1.2.10.0

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

Tested and got again error:

{无法配置Common.Logging from配置部分 'common/logging'."}

{"Could not configure Common.Logging from configuration section 'common/logging'."}

内部异常:

{在为以下内容创建配置节处理程序时发生错误 普通/日志:无法创建类型 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'(E:\ C# 项目\研究\ SPRING.NET \ SpringNet.Aspects \ LoggingWithLog4Net \ bin \ Debug \ LoggingWithLog4Net.vshost.exe.Config 第18行)}

{"An error occurred creating the configuration section handler for common/logging: Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net' (E:\C# PROJECTS\STUDY\SPRING.NET\SpringNet.Aspects\LoggingWithLog4Net\bin\Debug\LoggingWithLog4Net.vshost.exe.Config line 18)"}

StackTrace:

StackTrace:

{无法创建类型 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'}

{"Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'"}

我真的很困惑我做错了什么?程序集版本有问题吗?

I am really confuse what I do bad? Some problem with assemblies version?

推荐答案

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

For Spring.NET 1.3.2 you should use:

  • 常用日志记录2.0.0
  • Common.Logging.Log4Net 2.0.0
  • log4net 1.2.10

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

Make sure the last two log4net dll's are copied to the output directory. Since your code doesn't reference it directly, they might not be copied by Visual Studio.

这篇关于如何使用log4net在Spring.NET中配置日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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