C# WPF中的DispatcherUnhandledExceptionEventHandler捕获异常是visual studio,但未在客户端计算机中写入日志

C# WPF中的DispatcherUnhandledExceptionEventHandler捕获异常是visual studio,但未在客户端计算机中写入日志,c#,wpf,exception-handling,C#,Wpf,Exception Handling,我们有一个WPF windows桌面客户端应用程序,我在其中编写了处理全局未处理异常和线程级异常以及所有异常的逻辑 但是当我尝试使用Null引用异常并使用visualstudio在我的开发机器中正确地写入日志时,但是当我们发布此应用程序并且用户由于一些未处理的异常而在其机器中安装时,应用程序崩溃,并且没有在用户机器中写入日志 我不知道我在这里错过了什么 <Application x:Class="MyApp.App" xmlns="http://schemas.micr

我们有一个WPF windows桌面客户端应用程序,我在其中编写了处理全局未处理异常和线程级异常以及所有异常的逻辑

但是当我尝试使用Null引用异常并使用visualstudio在我的开发机器中正确地写入日志时,但是当我们发布此应用程序并且用户由于一些未处理的异常而在其机器中安装时,应用程序崩溃,并且没有在用户机器中写入日志

我不知道我在这里错过了什么

 <Application x:Class="MyApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         ShutdownMode="OnExplicitShutdown"
         Startup="Application_Startup"
         DispatcherUnhandledException ="AppDispatcherUnhandledException">
<Application.Resources>
</Application.Resources>
</Application>

private void Application_Startup(object sender, StartupEventArgs e)
    {
        // Global exception handling
        try
        {
    AppDomain.CurrentDomain.UnhandledException += new 
  UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    System.Windows.Forms.Application.ThreadException += new 
   ThreadExceptionEventHandler(Application_ThreadException);
    Application.Current.DispatcherUnhandledException += new 

DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
        }
        catch (Exception exc)
        {
            Logger.LogEntry(LogModel.Create(this).Fatal("AppStart"));
            Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
        }
    }

  void CurrentDomain_UnhandledException(object sender, 
 UnhandledExceptionEventArgs args)
    {
        try
        {
            Exception exception = (Exception)args.ExceptionObject;

    Logger.LogEntry(LogModel.Create(this).Fatal("DomainLevel"));

  Logger.LogEntry(LogModel.Create(this).Fatal(exception.Message));

    Logger.LogEntry(LogModel.Create(this).Fatal(exception.StackTrace));
        }
        catch (Exception exc)
        {
            try
            {
                MessageBox.Show("Fatal Non-UI Error",
                    "Fatal Non-UI Error. Could not write the error to the 
  event log. Reason: "
                    + exc.Message);
            }
            finally
            {
                Logger.LogEntry(LogModel.Create(this).Fatal("Domain 
  Finally"));

  Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
            }
        }
    }

  void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        // Log the exception, display it, etc
        try
        {
            Logger.LogEntry(LogModel.Create(this).Fatal("AppThread 
 Try"));

 Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.Message));

 Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.StackTrace));
        }
        catch (Exception exc)
        {
            Logger.LogEntry(LogModel.Create(this).Fatal("AppThread 
Catch"));
            Logger.LogEntry(LogModel.Create(this).Fatal(exc.Message));
            MessageBox.Show("Fatal Non-UI Error",
                    "Fatal Non-UI Error. Could not write the error to the 
event log. Reason: "
                    + exc.Message);
        }
    }

    void AppDispatcherUnhandledException(object sender, 
DispatcherUnhandledExceptionEventArgs e)
    {
        {
           // #if DEBUG   // In debug mode do not custom-handle the 
  exception, let Visual Studio handle it
            try
            {
                e.Handled = false;

               // #else

                ShowUnhandledException(e);

             //    #endif
            }
            catch (Exception exc)
            {

    Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
                MessageBox.Show("Fatal Non-UI Error",
                        "Fatal Non-UI Error. Could not write the error to 
   the event log. Reason: "
                        + exc.Message);
            }
        }
    }
    void ShowUnhandledException(DispatcherUnhandledExceptionEventArgs e)
    {
        try
        {
            Logger.LogEntry(LogModel.Create(this).Fatal("Dispatcher 
try"));

Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.Message));

Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.StackTrace));
            e.Handled = true;
        }
        catch (Exception exc)
        {
            Logger.LogEntry(LogModel.Create(this).Fatal("Dispatcher 
 Catch"));
            Logger.LogEntry(LogModel.Create(this).Fatal(exc.Message));
            MessageBox.Show("Fatal Non-UI Error",
                    "Fatal Non-UI Error. Could not write the error to the 
event log. Reason: "
                    + exc.Message);
        }
    }

    public static void LogEntry(ILogEntry entry)
    {
        var log = LogManager.GetLogger(entry.SourceComponentName);

        var message = string.Format("{0}::L{1} {2} - {3}", entry.SourceMemberName, entry.SourceLineNumber, entry.Text, entry.SourceFilePath.Split('\        \').LastOrDefault());

        else if (entry.Level == LogLevelState.Error)
        {
            log.Error(message);
        }
        else if (entry.Level == LogLevelState.Fatal)
        {
            log.Fatal(message);
        }
       #if DEBUG
        Console.WriteLine(message);
#endif
    }

        public static LogModel Create<T>(T source = default(T), [CallerMemberName] string callerMemberName = null, [CallerFilePath] string      callerFilePath = null, [CallerLineNumber] int callerLineNumber = 0)
    {
        return new LogModel
        {
            SourceComponentName = typeof(T).Name,
            SourceMemberName = callerMemberName,
            SourceFilePath = callerFilePath,
            SourceLineNumber = callerLineNumber
        };
    }

public LogModel Fatal(string format, params object[] args)
    {
        Level = LogLevelState.Fatal;
        Text = string.Format(format, args);

        return this;
    }

私有void应用程序\u启动(对象发送方、StartupEventArgs e)
{
//全局异常处理
尝试
{
AppDomain.CurrentDomain.UnhandledException+=新建
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
System.Windows.Forms.Application.ThreadException+=新建
ThreadExceptionEventHandler(应用程序\u ThreadException);
Application.Current.DispatcherUnhandledException+=新建
DispatcherUnandledExceptionEventHandler(AppDispatcherUnandledException);
}
捕获(异常exc)
{
Logger.LogEntry(LogModel.Create(this.Fatal)(“AppStart”);
Logger.LogEntry(LogModel.Create(this.Fatal)(exc.StackTrace));
}
}
void CurrentDomain\u未处理异常(对象发送方,
未处理的异常事件(args)
{
尝试
{
异常异常=(异常)args.ExceptionObject;
Logger.LogEntry(LogModel.Create(this.Fatal)(“域级别”);
LogEntry(LogModel.Create(this.Fatal)(exception.Message));
LogEntry(LogModel.Create(this.Fatal)(exception.StackTrace));
}
捕获(异常exc)
{
尝试
{
MessageBox.Show(“致命的非UI错误”,
“致命的非UI错误。无法将错误写入
事件日志。原因:“
+执行消息);
}
最后
{
Logger.LogEntry(LogModel.Create(this).Fatal(“域
最后),;
Logger.LogEntry(LogModel.Create(this.Fatal)(exc.StackTrace));
}
}
}
无效应用程序\u ThreadException(对象发送方,ThreadExceptionEventArgs e)
{
//记录异常、显示异常等
尝试
{
Logger.LogEntry(LogModel.Create(this).Fatal(“AppThread
尝试),;
LogEntry(LogModel.Create(this.Fatal)(例如Exception.Message));
LogEntry(LogModel.Create(this.Fatal)(例如Exception.StackTrace));
}
捕获(异常exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal(“AppThread
渔获量),;
Logger.LogEntry(LogModel.Create(this.Fatal)(exc.Message));
MessageBox.Show(“致命的非UI错误”,
“致命的非UI错误。无法将错误写入
事件日志。原因:“
+执行消息);
}
}
无效AppDispatcherUnhandledException(对象发送方,
DispatcherUnhandledExceptionEventArgs e)
{
{
//#如果调试//处于调试模式,请不要自定义处理
异常,让Visual Studio处理它
尝试
{
e、 已处理=错误;
//#其他
显示未处理的异常(e);
//#endif
}
捕获(异常exc)
{
Logger.LogEntry(LogModel.Create(this.Fatal)(exc.StackTrace));
MessageBox.Show(“致命的非UI错误”,
“致命的非UI错误。无法将错误写入
事件日志。原因:
+执行消息);
}
}
}
无效显示未处理的异常(DispatcherUnhandledExceptionEventArgs e)
{
尝试
{
Logger.LogEntry(LogModel.Create(this).Fatal(“调度器
尝试),;
LogEntry(LogModel.Create(this.Fatal)(例如Exception.Message));
LogEntry(LogModel.Create(this.Fatal)(例如Exception.StackTrace));
e、 已处理=正确;
}
捕获(异常exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal(“调度器
渔获量),;
Logger.LogEntry(LogModel.Create(this.Fatal)(exc.Message));
MessageBox.Show(“致命的非UI错误”,
“致命的非UI错误。无法将错误写入
事件日志。原因:“
+执行消息);
}
}
公共静态无效日志条目(ILogEntry条目)
{
var log=LogManager.GetLogger(entry.SourceComponentName);
var message=string.Format(“{0}::L{1}{2}-{3}”,entry.SourceMemberName,entry.SourceLineNumber,entry.Text,entry.SourceFilePath.Split('\\').LastOrDefault());
else if(entry.Level==LogLevelState.Error)
{
日志错误(消息);
}
else if(entry.Level==LogLevelState.Fatal)
{
log.Fatal(消息);
}
#如果调试
控制台写入线(消息);
#恩迪夫
}
公共静态日志模型创建(T源=默认值(T),[CallerMemberName]字符串CallerMemberName=null,[CallerFilePath]字符串CallerFilePath=null,[CallerLineNumber]int CallerLineNumber=0)
{
返回新的日志模型
{
SourceComponentName=typeof(T).Name,
SourceMemberName=callerMemberName,
SourceFilePath=callerFilePath,
SourceLineNumber=callerLineNumber
};
}
公共日志模型致命(字符串格式,参数对象[]args)
{
Level=LogLevelState.Fatal;
Text=string.Format(格式,args);
归还这个;
}
编辑:

在EventViewer中

第一项活动:

错误2018年5月15日11:32:52 PM.NET运行时1026无

应用程序:MyApp.exe 框架版本:v4.0.30319 描述:由于未处理的异常,进程已终止。 除了