How to get result of Null Reference Analysis into a log file(如何将空引用分析的结果写入日志文件)
问题描述
我有以下课程.
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.
这篇关于如何将空引用分析的结果写入日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将空引用分析的结果写入日志文件
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
