如何登录异常信息,以便解决问题 [英] How do I log exception information to allow troubleshooting?

查看:141
本文介绍了如何登录异常信息,以便解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做维修的Windows服务,并在在code某些点有一定的异常处理(例如,从定时器等外部事件的回调):

 尝试{
  ...
}
赶上(例外前){
   _logger.Log(未处理的异常:{0} \ r \ N {1},ex.Message,ex.StackTrace);
   Environment.FailFast(严重错误。);
}
 

记录异常的信息可以帮助排除故障问题出在哪里。但是,有时有趣的信息是内部异常,这使得它很难确定问题的根本原因。例如,一个 TypeInitializationException 可能很难理解。

有没有更好的方式来记录异常的信息进行故障排除?

解决方案
  

有没有更好的方式来记录异常的信息进行故障排除?

是有。不要被聪明,并使用 ex.Message ex.StackTrace 。只需使用 ex.ToString()。它将递归到内部异常(如需要多级),并显示完整的堆栈跟踪。

 尝试{
  ...
}
赶上(例外前){
   _logger.Log(未处理的异常:\ r \ N {0},前);
   Environment.FailFast(严重错误。);
}
 

要提供一个小例子,这是如果你创建抛出在类的静态构造函数的异常类的实例,你会得到什么。此异常抛出将被包裹在一个 TypeInitializationException

未处理的异常:的类型初始SomeClass的'引发了异常。
   在SomeNamespace.SomeClass..ctor()
   在SomeNamespace.Callback()在C:\ ExceptionHandlingDemo.cs:行34

不是非常有帮助。这是很难判断哪里出了问题。

未处理的异常:
System.TypeInitializationException:的类型初始SomeClass的'引发了异常。 ---> System.ArgumentException:具有相同键的项已被添加。
   在System.ThrowHelper.ThrowArgumentException(ExceptionResource资源)
   在System.Collections.Generic.Dictionary`2.Insert(TKEY的关键,TValue值,布尔加)
   在System.Collections.Generic.Dictionary`2.Add(TKEY的关键,TValue值)
   在SomeNamespace.SomeClass..cctor()在C:\ ExceptionHandlingDemo.cs:第43行
   ---内部异常堆栈跟踪的结尾---
   在SomeNamespace.SomeClass..ctor()
   在SomeNamespace.Callback()在C:\ ExceptionHandlingDemo.cs:行34

现在,你可以很容易地判断问题是在字典中的重复键的根本原因,并找出其行43源文件中的

I'm currently doing maintenance on a Windows service and at certain points in the code there is some exception handling (e.g. in callbacks from timers and other external events):

try {
  ...
}
catch (Exception ex) {
   _logger.Log("Unhandled exception: {0}\r\n{1}", ex.Message, ex.StackTrace);
   Environment.FailFast("Fatal error.");
}

Logging the exception information helps troubleshooting what went wrong. However, sometimes the interesting information is the inner exception which makes it hard to determine the root cause of the problem. For instance a TypeInitializationException can be hard to understand.

Is there a better way to log exception information for troubleshooting purposes?

解决方案

Is there a better way to log exception information for troubleshooting purposes?

Yes there is. Don't be "smart" and use ex.Message and ex.StackTrace. Just use ex.ToString(). It will recurse into inner exceptions (multiple levels if required) and show the complete stacktrace.

try {
  ...
}
catch (Exception ex) {
   _logger.Log("Unhandled exception:\r\n{0}", ex);
   Environment.FailFast("Fatal error.");
}

To provide a small example, this is what you get if you create an instance of a class that throws an exception in the static constructor of the class. This exception throw will be wrapped in a TypeInitializationException.

Before:

Unhandled exception: The type initializer for 'SomeClass' threw an exception.
   at SomeNamespace.SomeClass..ctor()
   at SomeNamespace.Callback() in c:\ExceptionHandlingDemo.cs:line 34

Not very helpful. It is hard to determine what went wrong.

After:

Unhandled exception:
System.TypeInitializationException: The type initializer for 'SomeClass' threw an exception. ---> System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at SomeNamespace.SomeClass..cctor() in c:\ExceptionHandlingDemo.cs:line 43
   --- End of inner exception stack trace ---
   at SomeNamespace.SomeClass..ctor()
   at SomeNamespace.Callback() in c:\ExceptionHandlingDemo.cs:line 34

Now you can quite easily determine the root cause of the problem being a duplicate key in a dictionary and pinpoint it to line 43 in the source file.

这篇关于如何登录异常信息,以便解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆