过去,我很幸运能够使用如下代码将异常记录到 Web 服务(只要允许客户端注销到 Internet)。这是用于记录您尚未捕获的任何内容。如果您在发布模式下编译应用程序但还包含 pdb 文件,您将获得带有行号的堆栈跟踪。
您还应该记录程序集的版本,以了解哪个版本的应用程序给您带来错误。
public void RegisterHandlers()
{
Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
}
private void UnhandledExceptionFunction(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
ExceptionLogger(e.StackTrace);
}
private void ThreadExceptionFunction(object sender, ThreadExceptionEventArgs args)
{
ExceptionLogger(args.Exception.StackTrace);
}
private void ExceptionLogger(string trace)
{
// log the message to a webservice
}