如何防止导致应用程序崩溃的异常?
How to prevent an exception which causes application crash?
我有一个 C# .NET 4.5.2 windows 表单应用程序,它是我使用任务和等待架构实现的。最近我在应用程序中看到一个奇怪的行为。尽管我使用了很多 try-catch 语句并且几乎每种异常都被处理了几次(每 3 或 4 天),但我收到了 X 停止工作的消息,应用程序中没有任何日志。
我在事件查看器中只有两个条目,第一个是 .NET 运行时类型,第二个是应用程序错误。
对于日志记录,我使用 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 Main 函数中放一个global try catch 对吗?
重点是您应该在应用启动后立即定义处理程序。
// Bind your handler asap aap starts
AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledException;
// Gereftish ;)
static void GlobalUnhandledException(object sender, UnhandledExceptionEventArgs e) { // here we have the exception }
我有一个 C# .NET 4.5.2 windows 表单应用程序,它是我使用任务和等待架构实现的。最近我在应用程序中看到一个奇怪的行为。尽管我使用了很多 try-catch 语句并且几乎每种异常都被处理了几次(每 3 或 4 天),但我收到了 X 停止工作的消息,应用程序中没有任何日志。
我在事件查看器中只有两个条目,第一个是 .NET 运行时类型,第二个是应用程序错误。
对于日志记录,我使用 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 Main 函数中放一个global try catch 对吗?
重点是您应该在应用启动后立即定义处理程序。
// Bind your handler asap aap starts
AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledException;
// Gereftish ;)
static void GlobalUnhandledException(object sender, UnhandledExceptionEventArgs e) { // here we have the exception }