<p>As part of some error handling in our product, we'd like to dump some stack trace information. However, we experience that many users will simply take a screenshot of the error message dialog instead of sending us a copy of the full report available from the program, and thus I'd like to make some minimal stack trace information available in this dialog.</p>

<p>A .NET stack trace on my machine looks like this:</p>

<pre><code>at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path)
at LVKWinFormsSandbox.MainForm.button1_Click(Object sender, EventArgs e) in C:\Dev\VS.NET\Gatsoft\LVKWinFormsSandbox\MainForm.cs:line 36
</code></pre>

<p>I have this question:</p>

<p>The format looks to be this:</p>

<pre><code>at &lt;class/method&gt; [in file:line ##]
</code></pre>

<p>However, the <em>at</em> and <em>in</em> keywords, I assume these will be localized if they run, say, a norwegian .NET runtime instead of the english one I have installed.</p>

<p>Is there any way for me to pick apart this stack trace in a language-neutral manner, so that I can display only the file and line number for those entries that have this?</p>

<p>In other words, I'd like this information from the above text:</p>

<pre><code>C:\Dev\VS.NET\Gatsoft\LVKWinFormsSandbox\MainForm.cs:line 36
</code></pre>

<p>Any advice you can give will be helpful.</p>

[{'answer_id': 51803, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 7, 'selected': True, 'text': '<p>You should be able to get a StackTrace object instead of a string by saying</p>\n\n<pre><code>var trace = new System.Diagnostics.StackTrace(exception);\n</code></pre>\n\n<p>You can then look at the frames yourself without relying on the framework\'s formatting.</p>\n\n<p>See also: <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx" rel="noreferrer">StackTrace reference</a></p>\n'}, {'answer_id': 51821, 'author': 'oglester', 'author_id': 2017, 'author_profile': 'https://Stackoverflow.com/users/2017', 'pm_score': 1, 'selected': False, 'text': '<p>As alternative, log4net, though potentially dangerous, has given me better results than System.Diagnostics.   Basically in log4net, you have a method for the various log levels, each with an Exception parameter.  So, when you pass the second exception, it will print the stack trace to whichever appender you have configured. </p>\n\n<p>example: <code>Logger.Error("Danger!!!", myException );</code></p>\n\n<p>The output, depending on configuration, looks something like</p>\n\n<pre><code>System.ApplicationException: Something went wrong.\n   at Adapter.WriteToFile(OleDbCommand cmd) in C:\\Adapter.vb:line 35\n   at Adapter.GetDistributionDocument(Int32 id) in C:\\Adapter.vb:line 181\n   ...\n</code></pre>\n'}, {'answer_id': 1239123, 'author': 'Lindholm', 'author_id': 122253, 'author_profile': 'https://Stackoverflow.com/users/122253', 'pm_score': 5, 'selected': False, 'text': '<p>Here is the code I use to do this without an exception</p>\n\n<pre><code>public static void LogStack()\n{\n  var trace = new System.Diagnostics.StackTrace();\n  foreach (var frame in trace.GetFrames())\n  {\n    var method = frame.GetMethod();\n    if (method.Name.Equals("LogStack")) continue;\n    Log.Debug(string.Format("{0}::{1}", \n        method.ReflectedType != null ? method.ReflectedType.Name : string.Empty,\n        method.Name));\n  }\n}\n</code></pre>\n'}, {'answer_id': 6088221, 'author': 'Hertzel Guinness', 'author_id': 293974, 'author_profile': 'https://Stackoverflow.com/users/293974', 'pm_score': 4, 'selected': False, 'text': '<p>Just to make this a 15 sec copy-paste answer:</p>\n\n<pre><code>static public string StackTraceToString()\n{\n    StringBuilder sb = new StringBuilder(256);\n    var frames = new System.Diagnostics.StackTrace().GetFrames();\n    for (int i = 1; i &lt; frames.Length; i++) /* Ignore current StackTraceToString method...*/\n    {\n        var currFrame = frames[i];\n        var method = currFrame.GetMethod();\n        sb.AppendLine(string.Format("{0}:{1}",                    \n            method.ReflectedType != null ? method.ReflectedType.Name : string.Empty,\n            method.Name));\n    }\n    return sb.ToString();\n}\n</code></pre>\n\n<p>(based on Lindholm answer)</p>\n'}, {'answer_id': 8779644, 'author': 'Aftershock', 'author_id': 135807, 'author_profile': 'https://Stackoverflow.com/users/135807', 'pm_score': 3, 'selected': False, 'text': '<p>Or there is even shorter version..</p>\n\n<pre><code>Console.Write(exception.StackTrace);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51768', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/267/']