如何将空引用分析的结果写入日志文件

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.

这篇关于如何将空引用分析的结果写入日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)