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

后端 未结 3 1669
悲哀的现实
悲哀的现实 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 06:04

    I read the Austin Salonen's answer and it's obvious better, but I've already started with regex. so I'll write it anyway.

    Regex r = new Regex(@"at (?.*)\.(?.*)\.(?.*(.*)) in (?.*):line (?\d*)");
    var result = r.Match(@"at Foo.Core.Test.FinalMethod(Doh doh) in C:\Projects\src\Core.Tests\Test.cs:line 21");
    if (result.Success)
    {
        string _namespace = result.Groups["namespace"].Value.ToString();
        string _class = result.Groups["class"].Value.ToString();
        string _method = result.Groups["method"].Value.ToString();
        string _file = result.Groups["file"].Value.ToString();
        string _line = result.Groups["line"].Value.ToString();
        Console.WriteLine("namespace: " + _namespace);
        Console.WriteLine("class: " + _class);
        Console.WriteLine("method: " + _method);
        Console.WriteLine("file: " + _file);
        Console.WriteLine("line: " + _line);
    }
    

提交回复
热议问题