我想从Spring.NET Aspect库使用Logging方面和log4Net . 我发现this article如何将log4Net与Common Logging API一起使用 .
我的测试应用程序是控制台,基于.NET 4.0客户端配置文件 .
1 st尝试
所以我在我的项目中引用这些库:
-
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版本:1.2.10.0,运行时v2.0.50727
我在Program Files \ Spring.NET 1.3.2 \ bin \ net \ 4.0中找不到程序集Common.Logging.Log4Net.dll所以我下载了这个程序集from SourceForge:
- Common.Logging.Log4Net版本2.0.0.0运行时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");
我收到了这个运行时错误:
{“无法从配置部分'common / logging'配置Common.Logging . ”}
内部异常:
{“为common / logging创建配置节处理程序时出错:无法创建类型'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net'(E:\ C#PROJECTS \ STUDY \ SPRING.NET \ SpringNet . Aspects \ LoggingWithLog4Net \ bin \ Debug \ LoggingWithLog4Net.vshost.exe.Config line 18)“}
堆栈跟踪:
在System.Configuration.BaseConfigurationRecord.EvaluateOne(String []键,SectionInput输入,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult)处于System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult,Boolean getLkg System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String)上的System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject)中的布尔getRuntimeObject,Object&result,Object和resultRuntimeObject) 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 getRu在System.Configuration.ConfigurationManager的System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)中的System.Configuration.BaseConfigurationRecord.GetSection(String configKey)处的ntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject . 在Common.Logging.LogManager.BuildLoggerFactoryAdapter()的Common.Logging.ConfigurationReader.GetSection(String sectionName)的System.Configuration.ConfigurationSettings.GetConfig(String sectionName)中的GetSection(String sectionName)
2 nd尝试
我认为问题必须是程序集Common.Logging.Log4Net版本2.0.0.0运行时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 .
并再次测试 . 我收到了这个错误:
无法创建类型'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net'
内部异常:
{“无法加载文件或程序集'Common.Logging.Log4Net'或其中一个依赖项 . 系统找不到指定的文件 . ”:“Common.Logging.Log4Net”}
堆栈跟踪:
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 ,System.RuntimeType.GetType(String typeName,Boolean throwOnError,Boolean ignoreCase,Boolean reflectionOnly,StackCrawlMark&stackMark)处于System.Type.GetType(String typeName,Boolean throwOnError,Boolean ignoreCase)的Common.Logging.ConfigurationSectionHandler.ReadConfiguration中的布尔值loadTypeFromPartialName) (XmlNode部分)在c:\ CCNet \ netcommon \ trunk \ modules \ Common.Logging \ src \ Common \ Common.Logging \ Logging \ ConfigurationSectionHandler.cs:第130行
3 rd和最后一次尝试
最后我找到了装配在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
测试并再次出错:
{“无法从配置部分'common / logging'配置Common.Logging . ”}
内部异常:
{“为common / logging创建配置节处理程序时出错:无法创建类型'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net'(E:\ C#PROJECTS \ STUDY \ SPRING.NET \ SpringNet . Aspects \ LoggingWithLog4Net \ bin \ Debug \ LoggingWithLog4Net.vshost.exe.Config line 18)“}
堆栈跟踪:
{“无法创建类型'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net'”}
我真的很困惑我做的不好吗?程序集版本有些问题?
3 回答
对于Spring.NET 1.3.2,您应该使用:
Common.Logging 2.0.0
Common.Logging.Log4Net 2.0.0
log4net 1.2.10
确保最后两个
log4net
dll 's are copied to the output directory. Since your code doesn' t直接引用它,它们可能不会被Visual Studio复制 .而且,而不是
尝试
在您的配置文件中,sectionGroup名称是“spring”,而不是“spring” . 我从你的身上复制了我的工作并遇到了同样的问题 . 终于找到了原因 .
希望有所帮助!
另一种可能性是:
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修复了问题 .