6 log4net spring.net log4net-configuration
我想从Spring.NET Aspect库使用Logging方面和log4Net.我发现这篇文章如何将log4Net与Common Logging API一起使用.
我的测试应用程序是控制台,基于.NET 4.0客户端配置文件.
所以我在我的项目中引用这些库:
上面的程序集来自Program Files\Spring.NET 1.3.2\bin \net\4.0
我在Program Files\Spring.NET 1.3.2\bin \net\4.0中找不到程序集Common.Logging.Log4Net.dll 所以我从SourceForge下载这个程序集:
我在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>
Run Code Online (Sandbox Code Playgroud)
并试过这个:
ILog log = LogManager.GetLogger("myLogger");
log.Error("hello world");
Run Code Online (Sandbox Code Playgroud)
我收到了这个运行时错误:
{"无法从配置部分'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)"}
堆栈跟踪:
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, System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey)上的System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject)中的布尔getRuntimeObject,Object&result,Object和resultRuntimeObject) ,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject)at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRunt System.Configuration.ConfigurationManager上的System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)中的System.Configuration.BaseConfigurationRecord.GetSection(String configKey)处的imeObject,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
)
我认为问题必须是程序集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,布局loadTypeFromPartialName)在System.RuntimeType.GetType(String typeName,Boolean throwOnError,Boolean ignoreCase,Boolean reflectionOnly,StackCrawlMark&stackMark)处于System.Type.GetType(String typeName,Boolean throwOnError,Boolean ignoreCase)at Common.Logging.ConfigurationSectionHandler.ReadConfiguration( XmlNode部分)在c:\ CCNet \netcommon\trunk\modules\Common.Logging\src\Common\Common.Logging\Logging\ConfigurationSectionHandler.cs:第130行
最后我在spring例子中找到了汇编Common.Logging.Log4Net 1.2.0.2版,所以我用它.
测试并再次出错:
{"无法从配置部分'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'"}
我真的很困惑我做的不好吗?程序集版本有些问题?
对于Spring.NET 1.3.2,您应该使用:
确保将最后两个log4netdll复制到输出目录.由于您的代码不直接引用它,因此Visual Studio可能不会复制它们.