gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:09:11 24 4
gpt4 key购买 nike

我想将 Spring.NET Aspect 库中的日志记录方面与 log4Net 一起使用。我发现this article如何将 log4Net 与通用日志记录 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版本:1.2.10.0,运行时v2.0.50727

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

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

我在 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");

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

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

内部异常:

{"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)"}

堆栈跟踪:

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

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

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

Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, 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"}

堆栈跟踪:

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

第三次也是最后一次尝试

最后我在 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

测试并再次出现错误:

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

内部异常:

{"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)"}

堆栈跟踪:

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

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

最佳答案

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

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

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

关于log4net - 如何使用 log4net 在 Spring.NET 中配置日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8172623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com