如何从net Core中的异常获取行号和文件名? [英] How can I get the line number and file name from an exception in net Core?

查看:87
本文介绍了如何从net Core中的异常获取行号和文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我可以通过以下方式拉出我的行号和类

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

但是,无论何时我尝试获取该信息,现在都在.Net Core 1.0中使用类库,行号始终为0,文件名始终为null.

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.

我将needfileinfo布尔值设置为true,并且存在pdb文件.这是某些功能在NET Core框架中尚不可用,还是我现在应该采用一些其他方法?

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?

说出下面提供的答案.我已经在使用"System.Diagnostics.StackTrace":"4.0.1",并且具有相同的结果.空FileName和0 FileNumber.我什至开始了一个新项目,仅复制了他的代码和相同的结果.使用"System.Diagnostics.StackTrace"的文件名为零,文件号为零:"4.0.1"

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"

更新::

追求所有选择,我在不同的工作站上尝试过.而且效果很好.因此很明显,这是我的工作站上发生故障的原因.找出问题所在后,我将再次更新.

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.

因此,经过进一步的研究很有趣.尽管这是环保的,但它也特定于Net Core.在同一环境中的非Net Core应用程序中,StackTrace可以正常工作.但是我在事件日志中遇到了安全审核失败

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

每当我尝试从网络核心应用程序运行它时.显然,Core应用程序访问这些服务的方式与传统框架不同,并且与应用到系统的安全策略冲突.Ive与我们的Microsoft代表联系,寻求更多信息,以查看问题是否能够解决.将继续更新.

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.

推荐答案

在我的 project.json 中使用"System.Diagnostics.StackTrace":"4.0.1" 文件,我可以使用以下代码正确获取错误的行号和文件名:

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

这篇关于如何从net Core中的异常获取行号和文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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