如何防止导致应用程序崩溃的异常?

时间:2017-05-14 09:37:40

标签: c# winforms slack-api slack serilog

我有一个C#.NET 4.5.2 Windows窗体应用程序,我使用task和await架构实现了它。最近我在应用程序中看到了一个奇怪的行为。虽然我已经使用了很多try-catch语句,并且几乎每种情况都会被处理掉(每隔3或4天)我得到一条X停止工作的消息,而没有任何登录应用程序。

我只有两个条目在事件查看器中,第一个是.NET Runtime类型,第二个是Application Error。

对于日志记录,我使用登录到控制台的serilog和滚动文件。出于远程通信目的,我使用Slack(我认为这是问题的根源)。

以下是日志的内容:

    Application: X.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
   at SlackAPI.RequestState`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].GotResponse(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.CompleteCallback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.HttpWebRequest.SetResponse(System.Exception)
   at System.Net.HttpWebRequest.SetAndOrProcessResponse(System.Object)
   at System.Net.ConnectionReturnResult.SetResponses(System.Net.ConnectionReturnResult)
   at System.Net.Connection.CompleteConnectionWrapper(System.Object, System.Object)
   at System.Net.PooledStream.ConnectionCallback(System.Object, System.Exception, System.Net.Sockets.Socket, System.Net.IPAddress)
   at System.Net.ServicePoint.ConnectSocketCallback(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Faulting application name: X.exe, version: 1.5.0.0, time stamp: 0x5914053a
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x0655a904
Faulting process id: 0x1bb0
Faulting application start time: 0x01d2ca214de3853b
Faulting application path: PATH-TO-X.exe
Faulting module path: unknown
Report Id: 5ef2a70f-387a-11e7-9c66-000c29bbc8ee

为了与Slack通信,我使用SlackAPI。 有什么建议吗?

更新:如何处理线程和任务未处理的异常。在program.cs主函数中放置一个全局try catch是对的吗?

1 个答案:

答案 0 :(得分:2)

关键是你应该在应用程序启动时立即定义处理程序。

// Bind your handler asap aap starts
AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledException;

// Gereftish ;)
static void GlobalUnhandledException(object sender, UnhandledExceptionEventArgs e) { // here we have the exception }