How can I get line number and file name from exception in net core?

Before, I could print my line number and class for exception with the following

   System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
    var stackFrame = trace.GetFrame(trace.FrameCount - 1);
    var lineNumber = stackFrame.GetFileLineNumber();
    var file = stackFrame.GetFileName();

However, when working with the class library in .Net Core 1.0, whenever I try to extract this information, the line number is always 0, and the file name is always zero.

I set requirefileinfo boolean to true and pdb files are present. Is this some kind of feature that still does not work in the NET Core environment or is there some other approach that I should take now?

Speak with the answer below. I already use "System.Diagnostics.StackTrace": "4.0.1" and having the same result. Null FileName and 0 FileNumber. I even started a new project, duplicating only its code and the same result. Null File Name and 0 File Number using "System.Diagnostics.StackTrace": "4.0.1"

UPDATE ::

Performing all the options, I might think that I tried this on another workstation. And it works great. So, obviously, there is something on my workstation that fails ... So the problem is with the environment, not the specific package. I will update again when I find out what the problem is.

, . , Net Core. Net Net Core StackTrace .

A privileged service was called.

Service:
    Server: Security
    Service Name:   -

Process:
    Process ID: 0x55a0
    Process Name:   C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger\x64\msvsmon.exe

Service Request Information:
    Privileges:     SeCreateGlobalPrivilege

and  

A privileged service was called.


Service:
    Server: Security
    Service Name:   -

Process:
    Process ID: 0x5628
    Process Name:   C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\14.0.0\IntelliTrace.exe

Service Request Information:
    Privileges:     SeCreateGlobalPrivilege

. , Core -, , , . Ive Microsoft, , , . .

+3
1

"System.Diagnostics.StackTrace": "4.0.1" project.json , :

class Program
{
    static void Main(string[] args)
    {
        var test = new Test();
        try
        {
            test.TriggerError();
        }
        catch(Exception ex)
        {
            var trace = new StackTrace(ex, true);
            var frame = trace.GetFrames().Last();
            var lineNumber = frame.GetFileLineNumber();
            var fileName = frame.GetFileName();
        }

    }
}

class Test
{
    public void TriggerError()
    {
        throw new Exception();
    }
}
+4

Source: https://habr.com/ru/post/1660523/


All Articles