【问题标题】:log4net logging UnhandledException exceptions from different projectslog4net 记录来自不同项目的 UnhandledException 异常
【发布时间】:2015-02-18 02:17:48
【问题描述】:

我的解决方案中有几个项目,其中大部分是 Windows 服务。我为每个配置了 log4net(为每个生成一个单独的日志文件),以及用于 log4net 的 Raygun appender。我想为每个项目捕获 UnhandledException 并了解它们的来源(在日志文件和 raygun 仪表板中),并且我想在一个地方为我的解决方案中的所有项目执行此操作。

因此,我创建了一个单独的静态类和方法来记录这些异常。

  • 如何记录异常,以便我的日志文件显示生成该错误的类而不是 Logger 静态类(以及让 raygun.io 仪表板显示异常的正确来源)=>

    Logger.UnhandledException [错误]- 现在这是错误的。它应该是异常起源的类名

  • 我会为此创建一个线程安全的单例吗?这甚至是正确的方法吗?

静态类:

public static class Logger
{
    private static readonly ILog Log = LogManager.GetLogger(Assembly.GetEntryAssembly().FullName);

    public static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Log.Error(e);
    }
}

Windows 服务:

private static void Main(string[] args)
{

        Configure.Serialization.Xml();

        // Unhandled exceptions - subscribe to the event (I would do this to all my projects in the solution)
        AppDomain.CurrentDomain.UnhandledException += Logger.UnhandledException;
}

【问题讨论】:

    标签: c# log4net log4net-configuration raygun.io raygun


    【解决方案1】:

    没有办法获取发生未处理异常的类名,除非可能通过在事件处理程序中使用堆栈上的反射,在发布版本中无论如何都不能保证为您提供类名。

    未处理的异常应该在具有适当错误处理的 Windows 服务中异常:将您的 Main 代码包装在 try..catch 子句中,并在服务代码中可能发生的任何地方捕获异常。如果仍然发生未处理的异常,您至少可以使用堆栈跟踪,这将允许您确定异常的来源。

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2010-10-07
      • 2010-11-03
      • 1970-01-01
      相关资源
      最近更新 更多