C#未处理的异常处理程序,试图写入日志文件

问题描述

| 我的应用程序是Windows Forms .NET 4 C#TCP / IP服务器。每天都有一次普通Windows Server崩溃消息崩溃(按close关闭此应用程序)。我插入了以下代码以捕获可能导致此异常的任何异常,并将简单的null对象插入到例程中,该例程会定期调用以生成测试异常。 两件事情: 当发生测试异常时,在调试器中命中日志代码,创建并写入文件,一切都很好。 没有调试器,我将收到“继续或退出” .NET stacktrace消息,并且无论选择哪个选项,都不会创建或写入文件。 .NET消息对我毫无用处。我需要崩溃的日志文件堆栈跟踪。有人知道我该怎么做吗?谢谢。
static class Program
{
    [STAThread]
    static void Main(string[] rgszArgs)
    {
        //My exception handler
        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(CatchUnhandledException);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormMain(rgszArgs));
    }

    static void CatchUnhandledException
        (object sender,UnhandledExceptionEventArgs e)
    {
        StreamWriter sw;
        DateTime dtLogFileCreated = DateTime.Now;
        Exception ex;

        try
        {
            sw = new StreamWriter(\"crash-\" + dtLogFileCreated.Day + dtLogFileCreated.Month
                        + dtLogFileCreated.Year + \"-\" + dtLogFileCreated.Second
                        + dtLogFileCreated.Minute + dtLogFileCreated.Hour + \".txt\");

            ex = (Exception)e.ExceptionObject;

            sw.WriteLine(\"### Server Crash ###\");
            sw.WriteLine(ex.Message + ex.StackTrace);
            sw.Close();
        }
        finally
        {
            Application.Exit();
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)