问题描述
我有以下课程.
class Tim
{
public Tim()
{
}
public Tom GetTom()
{
return new Tom();
}
}
class Tom
{
public Tom()
{
}
public Jim GetJim()
{
return new Jim();
}
}
class Jim
{
public Jim()
{
}
public Jom GetJom()
{
return null;
}
}
class Jom
{
public Jom()
{
}
public string GetMagicString()
{
return "Hello World";
}
}
它的用法如下.
class Program
{
static void Main(string[] args)
{
new Tim().GetTom().GetJim().GetJom().GetMagicString();
}
}
当我运行它时,会抛出空引用异常,新的空引用分析结果在新的异常帮助器中显示得很漂亮,如下所示.
When I run it, a Null Reference exception is thrown and new Null Reference Analysis results shows beautifully in the new Exception Helper as shown below.
这很好,因为它清楚地向我展示了这个链中的哪个方法调用导致了异常.但是,如果这段代码在现场运行,我也想在日志文件中记录这个错误.为此,当我在 catch 块中捕获 NullReferenceException 时,我没有得到有关返回 null 的方法的信息.这是否仅在 VS Exception Helper 对话框中可用并且此时无法记录?
This is great as it clearly shows me which method call in this chain is responsible for the exception. However, if this piece of code is running in field, I would also like to log this error in log file also. For that, when I capture the NullReferenceException in a catch block, I am not getting this info about the method that returned a null. Is this something only available in VS Exception Helper dialog only and cannot be logged at this point?
推荐答案
这是否仅在 VS Exception Helper 对话框中可用并且此时无法记录?
Is this something only available in VS Exception Helper dialog only and cannot be logged at this point?
是的,这仅在 Visual Studio 异常帮助程序中可用,此时无法记录.
Yes, this is something only available in the Visual Studio Exception Helper and cannot be logged at this point.
这并非不可能,但您必须使用与 Visual Studio 调试器等效的功能来检测您的代码.在正常的、非仪器化的场景中,没有关于 null 引用实际来自何处的记录,因此您的日志输出无法提供该信息.
It's not literally impossible, but you would have to instrument your code with the functional equivalent to the Visual Studio debugger. In a normal, non-instrumented scenario, there is no record of where the null reference actually came from, so no way for your logged output to provide that information.
这篇关于如何将空引用分析的结果写入日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!