-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: [StackTraceHidden(StackTraceVisibility.HideAllChildren)]
#112130
Comments
Tagging subscribers to this area: @tommcdon |
Internal calls are in the overwhelming majority of cases very valuable in diagnosing the cause of an exception. Trivial methods like throw helpers can already be skipped from a stack trace, but a way to hide all subsequent stack frames is a step too far and I cannot find a valid use case for it. |
In your example program, would you decorate the The desired effects of the attribute don't seem trivial to define. The stack trace output by the following program currently shows only C.M3() and C.M2(), not C.Main(). If C.Main() were decorated with using System;
class C
{
//[StackTraceHidden(StackTraceVisibility.HideAllChildren)]
static void Main()
{
M2();
}
static void M2()
{
try
{
M3();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
static void M3()
{
throw new Exception("what?");
}
} |
You could always define an extension method to hide these entries. For example, this extensions method will only print lines associated with a source file. try
{
DoTest();
}
catch (Exception ex)
{
Console.WriteLine(ex.UserStackTrace());
}
static class ExceptionExtensions
{
public static string UserStackTrace(this Exception exception)
{
int offset = 0;
ReadOnlySpan<char> stackTrace = exception.StackTrace;
foreach (var line in stackTrace.EnumerateLines())
{
if (line.IndexOf(" in ") > 0)
{
break;
}
offset += line.Length + Environment.NewLine.Length;
}
return stackTrace[offset..].ToString();
}
} Note: I've only thought about this for about 5 minutes so the test for relevant lines might need to be a bit more sophisticated. |
Background and motivation
.NET stack traces are an important element that helps developers diagnose bugs and exceptions. However, diagnosing the actual location of the bug often involves developers skipping over unnecessary entries that should be hidden from the stack trace.
To alleviate this issue, the attribute
[StackTraceHidden()]
can be used to hide a single current method from the stack trace. This is often used for throw helpers but is an incomplete solution due to there being no ability to prune an entire branch of code.Take the following program as an example:
When this crashes, the stack trace will show:
The important part of the stack trace really begins at line 6 as lines 2-5 are essentially internal calls.
.NET should expose a way of easily hiding internal stack trace trees.
API Proposal
API Usage
Alternative Designs
Developers could catch and re-throw exceptions to try to clean up stack traces.
Risks
It is possible that someone might actually want all the internal details of exactly where an internal call failed. A method should exist that can retrieve them.
The text was updated successfully, but these errors were encountered: