使用 Log4net 或 nlog 在 Spring.NET 中记录异常的最佳方法

发布于 2024-12-16 23:15:28 字数 3444 浏览 0 评论 0原文

我想知道 Spring.NET 中首选哪种日志异常方式以及原因。 我发现了两个常见的场景。

1.使用 IThrowAdvice。

我创建了抛出建议并在方法 AfterThrowing 中处理/记录异常。

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 (通用 Loggin API) 用于配置例如 Log4net 进行日志记录。

<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>

最后,为业务层中的对象创建一个代理。

  <!--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 库中的异常处理方面。

2.Spring.NET 的异常方面

我想为日志异常创建一个处理程序,该处理程序将使用 Log4net 记录器。

异常处理程序:

<object id="exLogHandler"
        type="Spring.Aspects.Exceptions.LogExceptionHandler, Spring.Aop">
    <property name="LogName" value="???"/>
    <property name="LogLevel" value="Error"/>
</object>

,然后在异常处理建议中使用此处理程序:

<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>

我不确定第二种方法是否好。也许这就是愚蠢。

是否可以配置 LogExceptionHandler 以使用 Log4net 记录器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2024-12-23 23:15:28

我不确定它是否是最好的,但是SimpleLoggingAdvice 为您记录异常情况。此外,您可以配置 SimpleLoggingAdvice 来记录执行时间、方法参数和返回值。配置看起来像这样(来自文档):

<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>

当然,你还是要配置代理工厂日志记录,但您已经知道如何做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文