I would like to know which way of log exception in Spring.NET is prefered and why. I found two common scenarios.
1.Use IThrowAdvice.
I created throws advice and in method AfterThrowing handle / log exception.
namespace Aspects
{
public class ExLogThrowsAdvice : IThrowsAdvice
{
private ILog _logger;
public ExLogThrowsAdvice()
{
_logger = LogManager.GetLogger("Error_file");
}
public void AfterThrowing(MethodInfo methodInfo,
Object []args, Object target, Exception exception)
{
_logger.Error(exception);
}
}
}
and use Common.Loggin API (Common Loggin API) for configuring for example Log4net for logging.
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<log4net>
<appender name="ErrorFileAppender"
type="log4net.Appender.FileAppender">
<file value="errors.txt"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date%newline%username%newline[%thread] %message %newline"/>
</layout>
</appender>
<root>
<level value="ERROR"/>
<appender-ref ref="ErrorFileAppender"/>
</root>
</log4net>
And last, create a proxy for the object in the businees layer.
<!--ex log advice-->
<object id="theExLogAdvice" type="Aspects.ExLogThrowsAdvice, ExceptionLogging"/>
<!--auto proxy creator-->
<object type="Spring.Aop.Framework.AutoProxy.TypeNameAutoProxyCreator, Spring.Aop">
<property name="TypeNames" value="Aspects*"/>
<property name="InterceptorNames">
<list>
<value>theExLogAdvice</value>
</list>
</property>
</object>
This is first concept. The second which I found is to use aspect fo exception handling from the Spring Aspect library.
2.Exception aspects from Spring.NET
I would like create a handler for log exception and this handler will use the Log4net logger.
Handler for exception:
<object id="exLogHandler"
type="Spring.Aspects.Exceptions.LogExceptionHandler, Spring.Aop">
<property name="LogName" value="???"/>
<property name="LogLevel" value="Error"/>
</object>
and then use this handler in exception handle advice:
<object id="exLogAspect"
type="Spring.Aspects.Exceptions.ExceptionHandlerAdvice, Spring.Aop">
<property name="ExceptionHandlerDictionary">
<dictionary>
<entry key="log" ref="exLogHandler"/>
</dictionary>
</property>
<property name="ExceptionHandlers">
<list>
<value>on exception name SomeException log 'Ex:' + #e</value>
</list>
</property>
I am not sure if second way is good. Maybe it is stupidity.
It is possible configure LogExceptionHandler to use the Log4net logger?