2011年8月11日 星期四

[C#] Using log4net in Spring.NET

Preparation:



In Reference, please add Common.Logging.dll and Common.Logging.Log4Net.dll.



Client codes:
using Common.Logging;
...
ILog log = LogManager.GetLogger("root");
log.Error("This is not an error!");



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>

<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>

<logger name="Spring">
<level value="DEBUG" />
</logger>
</log4net>

<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>

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

</configuration>



The console application should display something likes
2011-08-11 17:55:52,835 [10] ERROR root - This is not an error!
if everything is fine.

沒有留言:

張貼留言