How can I get the line number and file name from an exception in net Core?

By : Bastyon
Source: Stackoverflow.com
Question!

I used to be able to pull my line number and class for an 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 working with a class library in .Net Core 1.0 now whenever I try to pull that information the line number is always 0 and the filename is always null.

I am setting the needfileinfo boolean to true and the pdb files are present. Is this some feature that just doesn't work yet in a NET Core framework or is there some different approach I am supposed to be taking now?

To Speak to the Answer provided below. I am already using "System.Diagnostics.StackTrace": "4.0.1" and having the same result. Null FileName and 0 FileNumber. I even started a new project duplicating only his code and same result. Null File Name and 0 File Number using the "System.Diagnostics.StackTrace": "4.0.1"

UPDATE::

Pursuing every option I can think of I tried this on a different workstation. And it works fine. So apparently its something on my workstation that is failing...So the problem is environmental an not specific to the package. I will update again when I have figured out what the problem is.

So after further investigation interestingly enough. While it is environmental it is also specific to Net Core. in non Net Core applications in the same environment the StackTrace works fine. However I am getting security audit failures in my event log

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

whenever I try to run it from a net core app. Seems clear that the Core app access these services differently than legacy frameworks and its conflicting with security policies applied to the system. Ive contacted our Microsoft representative seeking more information to see if the issue cant be resolved. Will continue to update.

By : Bastyon


Answers

Using "System.Diagnostics.StackTrace": "4.0.1" in my project.json file I was able to properly acquire the line number and file name of an error with this code:

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();
    }
}
By : Will Ray


Both are O(n). Nothing else matters. Even their memory consumption will only differ minimally. You can do a benchmark yourself when you think that this is performance-critical, but it most likely isn't.

Use objects because they provide a clear structure for your data and make your code more readable. Yes, you should consider passing such an object to doSomething.

By : Bergi


With (not tested) :

awk '
  /\b[a-zA-Z]{5}\b/{
    matches[$0]++
  }
  END{
    for (m in matches) {
      if (matches[m] >= 3) {print m}
    }
  }
' file


This video can help you solving your question :)
By: admin