C# 使用 Exception.Data

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/144957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:29:51  来源:igfitidea点击:

Using Exception.Data

提问by Chris

How have you used the Exception.Data property in C# projects that you've worked on?

您如何在您处理过的 C# 项目中使用 Exception.Data 属性?

I'd like answers that suggest a pattern, rather than those that are very specific to your app.

我想要建议模式的答案,而不是那些非常特定于您的应用程序的答案。

采纳答案by Austin Salonen

The exception logger I use has been tweaked to write out all the items in the Data collection. Then for every exception we encounter that we cannot diagnose from the exception stack, we add in all the data in that function's scope, send out a new build, and wait for it to reoccur.

我使用的异常记录器已经过调整,可以写出数据集合中的所有项目。然后,对于我们无法从异常堆栈中诊断出的每个异常,我们添加该函数范围内的所有数据,发送一个新构建,并等待它再次发生。

I guess we're optimists in that we don't put it in every function, but we are pessimists in that we don't take it out once we fix the issue.

我想我们是乐观主义者,因为我们不会把它放在每个函数中,但我们是悲观主义者,因为一旦我们解决了问题,我们就不会把它去掉。

回答by Peter Meyer

I have used it when I knew the exception I was creating was going to need to be serialized. Using Reflector one day, I found that Excepion.Data gets stuck into and pulled from serialization streams.

当我知道我创建的异常需要被序列化时,我就使用了它。有一天使用 Reflector,我发现 Exception.Data 卡在序列化流中并从序列化流中拉出。

So, basically, if I have properties on a custom exception class that are already serializable types, I implement them on the derived class and use the underlying data object as their storage mechanism rather than creating private fields to hold the data. If properties of my custom exception object require more advanced serialization, I generally implement them using backing private fields and handle their serialization in the derived class.

所以,基本上,如果我在自定义异常类上有已经是可序列化类型的属性,我会在派生类上实现它们,并使用底层数据对象作为它们的存储机制,而不是创建私有字段来保存数据。如果我的自定义异常对象的属性需要更高级的序列化,我通常使用支持私有字段来实现它们并在派生类中处理它们的序列化。

Bottom line, Exception.Data gives you serialization for free just by sticking your properties into it -- but just remember those items need to be serializable!

最重要的是,Exception.Data 只需将您的属性粘贴到其中即可免费为您提供序列化——但请记住,这些项目需要可序列化!

回答by Ian Mercer

I've used it to capture information about the state at the time of the Exception from the enclosing scope as the Exception travels up the stack. Items like the filename that caused the Exception, or the value of some ID that will help track down the problem.

当异常沿堆栈向上移动时,我使用它从封闭范围捕获有关异常发生时的状态信息。诸如导致异常的文件名之类的项目,或者有助于追踪问题的某些 ID 的值。

At the top most level in a web application I also tend to add much of the Request information like the RawUrl, the cookies, the Referrer, ...

在 Web 应用程序的最顶层,我还倾向于添加许多请求信息,例如 RawUrl、cookie、Referrer、...

For more details here's my blog on the topic:

有关更多详细信息,请参阅我关于该主题博客

Rather than waiting for problems to happen I add this code in wherever an Exception can occur that's related to something external, e.g. a file name, or an URL that was being accessed, ... In other words, any data that will help repro the problem.

我不是等待问题发生,而是将这段代码添加到任何可能发生与外部相关的异常的地方,例如文件名或正在访问的 URL,......换句话说,任何有助于重现问题。

回答by Chris Marisic

Since none of the answers include any code. Something that might useful as an addition to this question is how to actually look at the .Datadictionary. Since it is not a generic dictionary and only returns IDictionary

由于没有一个答案包含任何代码。作为对这个问题的补充,可能有用的是如何实际查看.Data字典。由于它不是通用字典并且只返回IDictionary

foreach(var kvp in exception.Data)the type of kvp will actually be objectunhelpfully. However from the MSDNthere's an easy way to iterate this dictionary:

foreach(var kvp in exception.Data)kvp 的类型实际上是object无益的。然而,从MSDN有一个简单的方法来迭代这个字典:

foreach (DictionaryEntry de in e.Data)
    Console.WriteLine("    Key: {0,-20}      Value: {1}", 
                             "'" + de.Key.ToString() + "'", de.Value);

I don't really know what the format argument , -20would mean, maybe Take(20)? Digressing... this code can be very helpful in a common error logger to unwind this data. A more complete usage would be similar to:

我真的不知道格式参数, -20是什么意思,也许是 Take(20)?离题了……这段代码在一个常见的错误记录器中非常有用,可以解开这些数据。更完整的用法类似于:

var messageBuilder = new StringBuilder();

do
{                
    foreach (DictionaryEntry kvp in exception.Data)
        messageBuilder.AppendFormat("{0} : {1}\n", kvp.Key, kvp.Value);

    messageBuilder.AppendLine(exception.Message);


} while ((exception = exception.InnerException) != null);

return messageBuilder.ToString();