Pom*_*oma 26 .net c# exception-handling intellitrace
如何记录抛出和捕获的任何异常?像Visual Studio的IntelliTrace那样的东西.或者有没有办法将InteliTrace集成到应用程序的调试版本中,然后查看其日志?
更新:我会澄清一点.我想要标准的.txt(或任何自定义)日志,格式无关紧要.重点是我想记录所有第三方库中发生的所有异常,而不是向它们添加代码.
yas*_*891 29
我想你正在搜索的功能被调用FirstChanceException,可以通过AppDomain.FirstChanceException事件访问
本质上,这个事件提供了一个钩子来获取有关被抛出的任何(托管)异常的信息AppDomain.你不能这样处理异常!这只是一种通知
更新:关于你对另一个答案的"吞噬异常"的评论 - 只是一个黑暗的镜头:
在x64系统上,在Windows的onLoad方法中抛出的异常无法在Main()方法中捕获.
看这篇SO文章供参考
更新2:至于Threads我认为你必须自己实现它.这将涉及某种轮询并会损害性能,但我想在大多数情况下调试都没问题.这可以使用
var threads = Process.GetCurrentProcess().Threads;
Run Code Online (Sandbox Code Playgroud)
你可能会选择方面.例如,如果您使用PostSharp,并编写为每个函数创建try-catch的方面,则在方面中进行日志记录.登录后只需重新抛出即可.
来自他们网站的示例代码,以获得演示代码的完整答案:
/// <summary>
/// Aspect that, when applied on a method, catches all its exceptions,
/// assign them a GUID, log them, and replace them by an <see cref="InternalException"/>.
/// </summary>
[Serializable]
public class ExceptionPolicyAttribute : OnExceptionAspect
{
/// <summary>
/// Method invoked upon failure of the method to which the current
/// aspect is applied.
/// </summary>
/// <param name="args">Information about the method being executed.</param>
public override void OnException(MethodExecutionArgs args)
{
Guid guid = Guid.NewGuid();
Trace.TraceError("Exception {0} handled by ExceptionPolicyAttribute: {1}",
guid, args.Exception.ToString());
throw new InternalException(
string.Format("An internal exception has occurred. Use the id {0} " +
"for further reference to this issue.", guid));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
您可以使用任何记录器,如log4net,NLog或企业库(我首选的记录方式和其他一些东西).但这不是真正的任务.任务是 - 恕我直言 - 以尽可能少的手动编码注入项目.
| 归档时间: |
|
| 查看次数: |
12819 次 |
| 最近记录: |