问题
I have following classes.
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";
}
}
Its used as follows.
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.
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?
回答1:
Is this something only available in VS Exception Helper dialog only and cannot be logged at this point?
Yes, this is something only available in the Visual Studio Exception Helper and cannot be logged at this point.
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.
来源:https://stackoverflow.com/questions/42866640/how-to-get-result-of-null-reference-analysis-into-a-log-file