自定义log4net附加程序中的日志记录/错误处理 [英] logging/error handling inside a custom log4net appender

查看:105
本文介绍了自定义log4net附加程序中的日志记录/错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个内部开发的日志记录系统,我们希望获得log4net的好处,例如能够同时使用多个追加程序.我们决定将旧式日志记录系统移至自定义log4net附加程序中.当正确配置遗留系统时,这很好用,但是遗留记录器实际上写入了WCF客户端(这使得冗长的日志记录速度变慢,这是我们希望使用其他log4net附加程序的原因),最近WCF的配置并不完全是它本来应该.发生这种情况时,应用程序似乎可以正常运行,但是很明显,旧系统的自定义附加程序无法正常工作.其他附加程序工作正常,但是自定义旧式附加程序没有输出,也没有错误消息表明存在问题.

We have a internally-developed logging system, and we wanted to gain the benefits of log4net such as being able to use multiple appenders simultaneously. We decided to move the legacy logging system into a custom log4net appender. That worked great when the legacy system was configured correctly, but the legacy logger actually writes to a WCF client (making it slow for verbose logging, the reason we wanted to engage other log4net appenders), and recently the WCF configuration was not quite what it should have been. When this happened the application seemed to run ok, but obviously the custom appender for the legacy system was not working. The other appenders worked fine, but there was no output from the custom legacy appender and no error messages indicating that there was a problem.

处理递归"日志记录(从自定义log4net附加程序内部进行日志记录)的正确方法是什么?我尝试了一个幼稚的解决方案:从其静态对象中获取log4net并在附加程序中进行记录:

What is the proper way to handle "recursive" logging, that is logging from inside a custom log4net appender? I tried a naive solution: grabbing log4net from its static object and logging inside the appender:

public class MyCustomAppender : AppenderSkeleton
{

    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        try
        {
            // something which throws here...
        }
        catch (Exception ex)
        {
            log4net.LogManager.GetLogger(this.GetType()).Error(this.GetType().ToString() + ": error during append", ex);
        }
   }
}

这是一个严重的失败,"System.Threading.LockRecursionException:在此模式下不允许递归读取锁定获取."很公平;这是幼稚的解决方案.

This was an abject failure, "System.Threading.LockRecursionException: Recursive read lock acquisitions not allowed in this mode." Fair enough; it was the naive solution.

我也尝试过:

this.ErrorHandler.Error(this.GetType().ToString() + ": error during append", ex);

在异常处理程序中,看来这可能是对的.什么都没发生.

in the exception handler, as it seemed possible it might be the right thing. Nothing obvious happened.

有没有一种方法可以通过在附加程序内的log4net记录错误?还是我必须直接将一些硬代码写入Windows事件日志中?显然存在无限递归下降的巨大危险,但我认为可以通过某种方式让操作员知道某个附加程序未能运行.

Is there a way to log errors through log4net inside an appender? Or do I have to do something like hard-code writing to the windows event log directly? Obviously there is great danger of infinite recursive descent, but I would think there would be some way of letting the operator know that one of the appenders failed to run.

JR

推荐答案

当然,当我发布问题时,我就想起了什么. log4net在appSettings内部具有调试开关.我使用了以下代码:

Of course as soon as I posted the question, I remembered something. log4net has a debugging switch inside the appSettings. I used this code:

this.ErrorHandler.Error(this.GetType().ToString() + ": error during append", ex);

在catch块内,然后将魔术设置设置为:

inside the catch block, then set the magic setting on:

<appSettings>
    <!--this is wonderful magic-->
    <add key="log4net.Internal.Debug" value="true" />
</appSettings>

现在,ConsoleAppender(我正在使用的另一个附加程序)发布自定义附加程序异常消息.

Now the ConsoleAppender (the other appender I was using) put out the custom appender exception message.

JR

这篇关于自定义log4net附加程序中的日志记录/错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆