I tried to get the filename and sourceline number when an exception is thrown. But I got nothing.
class Program
{
static void Main(string[] args)
{
try
{
throw new InvalidOperationException();
}
catch (InvalidOperationException exception)
{
var stackTrace = new StackTrace(exception);
var currentFrame = stackTrace.GetFrame(0);
var fileName = currentFrame.GetFileName();
var sourceLineNumber = currentFrame.GetFileLineNumber();
Console.WriteLine("File Name: " + fileName);
Console.WriteLine("Source line number: " + sourceLineNumber);
Console.ReadKey();
}
}
}
There is only one frame is available. So I use the index 0 in GetFrame(index)
.