2

I was wondering is there is a good way of sub stringing parts of a StackTrace string.

Lets say I have a stacktrace showing this:

at TestApplicationInsightLogging.Controllers.TestController.PostEndpoint(TestPostModel input) in c:\users\john\documents\visual studio 2015\Projects\TestApplicationInsightLogging\TestApplicationInsightLogging\Controllers\TestController.cs:line 35

An I want to extract some information from that stack trace, such as:

  • Code-Line: "Line: 35"
  • Controller: "TestController"
  • Method: "PostEdnpoint(TestPostModel input)"

I know I can substring by counting letters ans stuff, but I don't thing that is dynamic enough for different kinds of stacktraces.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
H4p7ic
  • 1,669
  • 2
  • 32
  • 61
  • 4
    https://github.com/atifaziz/StackTraceParser – Daniel A. White Feb 12 '18 at 13:39
  • `The reason i want to do this is because i have a limited amount of characters to work with.` <= what do you mean by this? – Igor Feb 12 '18 at 13:39
  • Looks like some simple regex ... where you find this.that(parameters) and line x – BugFinder Feb 12 '18 at 13:39
  • What is the end goal of doing this? What happens with the information? – Igor Feb 12 '18 at 13:41
  • 1
    I know it was a bit unnecessary to write that but it just got put down when thinking of it. the reason is that i am logging this information in Application insights analytics and custom properties can only contain up to a 100 characters or something like that. when trying to insert the whole stacktrace it gets cut in half. @Igor – H4p7ic Feb 12 '18 at 13:44
  • @BugFinder ok so lets say i write a regex for line number for example. that's a number that would most likely be different depending on what error i get for example. How would you include any number as long as it is related to the "line" so to speak? – H4p7ic Feb 12 '18 at 13:47
  • 2
    If might be easier to work with individual frames instead of string. If you have instance of `Exception` - do `new StackTrace(exception)` and then explore each frame. – Evk Feb 12 '18 at 13:49
  • @john if you cant pull out "line ([0-9])+" then you need to read up on regex – BugFinder Feb 12 '18 at 13:54
  • @BugFinder Haha you're right about that, i just think i haven't quite yet understood how exactly regex works so to speak. But i'm learning! thats why i'm asking mate! ;) – H4p7ic Feb 12 '18 at 13:58
  • @Evk i think you're really on to something there that's a really good tip. didn't think about that. Thanks! – H4p7ic Feb 12 '18 at 13:59
  • Possible duplicate of [How to split a stacktrace line into namespace, class, method file and line number?](https://stackoverflow.com/questions/5148167/how-to-split-a-stacktrace-line-into-namespace-class-method-file-and-line-numbe) – Liam Feb 12 '18 at 14:55
  • You may want to take a look at the [Log Parser](https://technet.microsoft.com/en-us/scriptcenter/dd919274.aspx) tool. Add a layout to your logging framework (e.g. [CsvLayout for NLog](https://github.com/nlog/nlog/wiki/CsvLayout)) that formats the log as CSV, then you can parse it. – Georg Patscheider Feb 12 '18 at 15:05

0 Answers0