How to configure logging in Spring.NET using log4net?

I would like to use the Spring.NET Aspect Library Logging aspect along with log4Net. I found this article how to use log4Net with the Common Logging API.

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

1st attempt

Therefore, I refer to these libraries in my project:

  • Spring Version for version 1.3.2.40943, runtime v4.0.30319
  • Spring.AOP version 1.3.2.40943, runtime v4.0.30319
  • Common.Logging version: 1.2.0.0, runtime v1.0.3705

Assemblies are higher than the program files \ Spring.NET 1.3.2 \ bin \ net \ 4.0

  • log4net version: 1.2.10.0, runtime v2.0.50727

I could not find the 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 version 2.0.0.0 runtime v2.0.50727

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> 

and tried this:

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

I got this runtime error:

{"Failed to configure Common.Logging from the 'General / Logging' configuration section." }

Internal exception:

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

Stacktrace:

in System.Configuration.BaseConfigurationRecord.EvaluateOne (String [] keys, section entry, logical isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) in System.Configuration.BaseConfigurationRecord.Evaluate (FactoryRecordRecord factoryRecord sectionRecord Boolean getLkg, Boolean getRuntimeObject, Object & result, Object & resultRuntimeObject) in System.Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean getRuntimeObjectI & ObjectFunction & boolean Object & resultFunction .BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, object & resultRuntimeObject) in System.Configuration.BaseConfigurat ionRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, object & resultRuntimeObject) in System.Configuration.BaseConfigurationRecord.GetSection (StringConfiguration.connection.configuration.connection.configuration.connection.configuration.connect Internal.IInternalConfigSystem.GetSection (String sectionName) in System.Configuration.ConfigurationManager.GetSection (String sectionName) in System.Configuration.ConfigurationSettings.GetConfig (String sectionName) in Common.Logging.ConfigurationReader.GetSection String
in Common.Logging.LogManager.BuildLoggerFactoryAdapter ()

Second attempt

I think the problem should be in the build version of the 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.

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

And checked again. I got this error:

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

Internal exception:

{"Failed to load the file or assembly" Common.Logging.Log4Net "or one of its dependencies. The system cannot find the file indicated.": "Common.Logging.Log4Net"}

Stacktrace:

in 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 & amp; stackMark, Boolean loadTypeFromPartialName) when System.RuntimeType.GetType (String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark & ​​stackMarkleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleOrgeleer (en) .ConfigurationSectionHandler.ReadConfiguration (XmlNode section) in C: \ CCNet \ netcommon \ trunk \ Modules \ Common.Logging \ SRC \ Common \ Common.Logging \ Logging \ ConfigurationSectionHandler.cs: line 130

Third and final attempt

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

  • Common.Logging.Log4Net version 1.2.0.2
  • Common.Logging version 1.2.0.0
  • log4Net 1.2.10.0

Tested and error appeared again:

{"Failed to configure Common.Logging from the 'General / Logging' configuration section." }

Internal exception:

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

Stacktrace:

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

Am I really confusing what I'm doing badly? Some problem with the build version?

+6
source share
3 answers

For Spring.NET 1.3.2 you should use:

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

Make sure the last two log4net copied to the output directory. Since your code does not reference it directly, they cannot be copied using Visual Studio.

+5
source

Also, instead of

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

Try

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

In your configuration file, the name of the section is "spring", not "spring". I copied your work to do my work and got into the same problem. Finally figured out why.

Hope this helps!

+1
source

Another possibility:

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

I recommend skipping Common.Logging 2.1.0 since it threw this exception when reading the perfectly valid app.config section:

"Cannot overlay an object of type" System.Configuration.DefaultSection "onto type" System.Configuration.AppSettingsSection ".

Installation 2.1.1 fixes the problem.

+1
source

Source: https://habr.com/ru/post/901739/


All Articles