问题描述
我想知道在 Spring.NET 中记录异常的哪种方式是首选以及为什么.我发现了两种常见的情况.
I would like to know which way of log exception in Spring.NET is prefered and why.I found two common scenarios.
1. 使用 IThrowAdvice.
我在方法 AfterThrowing 中创建了 throws 建议和处理/记录异常.
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);
}
}
}
并使用 Common.Loggin API(Common Loggin API) 用于配置例如 Log4net 用于日志记录.
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>
这是第一个概念.我发现的第二个是使用方面来处理来自 Spring Aspect 库的异常处理.
This is first concept. The second which I found is to use aspect fo exception handling from the Spring Aspect library.
2.来自 Spring.NET 的异常方面
我想为日志异常创建一个处理程序,该处理程序将使用 Log4net 记录器.
I would like create a handler for log exception and this handler will use the Log4net logger.
异常处理程序:
<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.
是否可以配置 LogExceptionHandler 以使用 Log4net 记录器?
It is possible configure LogExceptionHandler to use the Log4net logger?
推荐答案
我不确定它是否是最好的,但是SimpleLoggingAdvice
为您记录异常.此外,您可以配置 SimpleLoggingAdvice
来记录执行时间、方法参数和返回值.配置看起来像这样(来自文档):
I'm not sure if it's the best, but a SimpleLoggingAdvice
logs exceptions for you. Furthermore, you can configure a SimpleLoggingAdvice
to log execution time, method arguments and return values. Configuration looks like this (from the docs):
<object name="loggingAdvice" type="Spring.Aspects.Logging.SimpleLoggingAdvice, Spring.Aop">
<property name="LogUniqueIdentifier" value="true"/>
<property name="LogExecutionTime" value="true"/>
<property name="LogMethodArguments" value="true"/>
<property name="LogReturnValue" value="true"/>
<property name="Separator" value=";"/>
<property name="LogLevel" value="Info"/>
<property name="HideProxyTypeNames" value="true"/>
<property name="UseDynamicLogger" value="true"/>
</object>
当然,你还是要配置代理工厂 和 日志记录,但您知道如何已经这样做了.
Of course, you still have to configure a proxy factory and logging, but you know how to do that already.
这篇关于使用 Log4net 或 nlog 在 Spring.NET 中记录异常的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!