繁体   English   中英

C#未处理的异常捕获器

[英]c# unhandled exceptions catcher

我们有一个包含多个项目的VS2013解决方案(本来是VS2012解决方案,但不相关)。 一些类库。 一些MVC应用程序。 一些WinForm应用程序。 一些comandline应用程序。 我们需要一个可以在所有项目中使用的类,以捕获未处理的异常并将它们记录到可配置的目标中(特定的应用程序可以决定是否要记录到平面文件,事件日志或db)。 想法是,在启动时,应用程序将调用静态方法或实例化对象,然后将所有未处理的异常神奇地记录到应用程序的其余部分。

我认为这可以解决问题(尽管第一次尝试只是记录到一个平面文件中):

    public class InfLogger
{
    public string LogFile { get; set; }
    public InfLogger()
    {
        System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ThreadExceptionHandler);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionsHandler);
        LogFile = "C:\\Infinedi.log";
    }

    public void ThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        try
        {
            if (File.Exists(LogFile))
            {
                File.AppendAllText(LogFile, e.Exception.Message + "\n");
            }
            else
            {
                File.WriteAllText(LogFile, e.Exception.Message + "\n");
            }
        }
        catch (Exception ex)
        {

        }
    }

    public void UnhandledExceptionsHandler(object sender, UnhandledExceptionEventArgs e)
    {
        try
        {
            if (File.Exists(LogFile))
            {
                File.AppendAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
            }
            else
            {
                File.WriteAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
            }
        }
        catch (Exception ex)
        {

        }
    }
}

}

但没有喜悦。

在我用来尝试对其进行测试的MVC应用程序中,我实例化了InfLogger对象,然后有目的地创建超出范围的未处理异常的数组索引。 但是记录器不会触发任何一个事件处理程序。 :(

关于如何做到这一点的任何建议?

我已经做过一些研究,实际上这对于控制台应用程序和Winforms确实有效。 但不是MVC。 嗯...不知道为什么。 但是,我发现了一个新问题。 这是使用代码的地方。

    namespace TestApp
    {
class Program
{
    static void Main(string[] args)
    {
        InfLogger logger = new InfLogger();
        int[] a = { 0, 1, 2 };
        int y = a[6];
    }
}
}

记录器确实捕获到异常。 但是执行只是挂在一行上:int y = a [6]。 它只是继续执行该行,抛出异常,一遍又一遍地捕获未处理的异常。 因此,现在我需要知道如何获得执行,以实际越过引发异常的行。

我在Program.cs文件中调用一个方法来设置未处理的异常方法。 这是MSDNApplication.SetUnhandledExceptionMode方法的完整说明。

// ....
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        SetUnhandledExceptionHandler();
        // ...

#region -- SetUnhandledExceptionsHandler() Method --
    /// <summary>
    /// Causes all unhandled exceptions to be handled by the application.
    /// This will not prevent the application from being forced to close, but
    /// does give a chance to log the error.
    /// </summary>
    public static void SetUnhandledExceptionHandler()
    {
        // Add an exception handler for all UI thread exceptions
        Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

        // Set the unhandled exception mode to force all Windows Forms errors to
        // Go through this handler
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling non-UI thread exceptions
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    }
#endregion

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM