如何将空引用分析的结果获取到日志文件中 [英] How to get result of Null Reference Analysis into a log file

查看:90
本文介绍了如何将空引用分析的结果获取到日志文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程.

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();
    }
}

当我运行它时,会抛出一个Null Reference异常,并且新的Null Reference Analysis结果在新的Exception Helper中显示精美,如下所示.

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时,我没有得到有关返回空值的方法的信息.这是仅在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调试器等效的功能来检测代码.在正常的非仪表化方案中,没有关于空引用实际来自何处的记录,因此您的日志输出无法提供该信息.

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.

这篇关于如何将空引用分析的结果获取到日志文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆