How to split a stacktrace line into namespace, class, method file and line number?

后端 未结 3 1672
悲哀的现实
悲哀的现实 2020-12-10 05:07

C# stack traces take the following form:

   at Foo.Core.Test.FinalMethod(Doh doh) in C:\\Projects\\src\\Core.Tests\\Test.cs:line 21
   at Foo.Core.Test.Anoth         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 05:42

    StackTraceParser can parse a stack trace text output (e.g. typically returned by Environment.StackTrace or Exception.StackTrace) back into a sequence of stack trace frames, including the following components:

    Type
    Method
    Parameter types and names
    File and line information, if present
    

    It is available as a NuGet source package that directly embeds into a C# project.

    However it requires a few functions to specify and it's usage is not immediately obvious.

    public static IEnumerable Parse(
        string text,
        Func tokenSelector,
        Func methodSelector,
        Func parameterSelector,
        Func, TParameters> parametersSelector,
        Func sourceLocationSelector,
        Func selector)
    

    See examples in https://github.com/atifaziz/StackTraceParser and https://bitbucket.org/project-elmah/main/src/2a6b0b5916a6b4913ca5af4c22c4e4fc69f1260d/src/Elmah.AspNet/ErrorDetailPage.cs?at=default&fileviewer=file-view-default

提交回复
热议问题