Skip to content

Commit cbb5b90

Browse files
authored
Make Exception.StackTrace resilient to missing attribute assemblies (#48535)
A customer has reported a problem when they could not get stack trace from an exception that they needed to log, because one of the methods on the stack or its defining type had an attribute from an assembly that was missing. This change fixes that by ignoring exceptions in attempts to get StackTraceHiddenAttribute, preferring to err on the side of getting a stack frame that was marked as hidden in the stack trace rather than not getting any stack trace at all.
1 parent e912151 commit cbb5b90

File tree

1 file changed

+19
-10
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Diagnostics

1 file changed

+19
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,28 @@ private static bool ShowInStackTrace(MethodBase mb)
354354
return false;
355355
}
356356

357-
if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
357+
try
358358
{
359-
// Don't show where StackTraceHidden is applied to the method.
360-
return false;
361-
}
359+
if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
360+
{
361+
// Don't show where StackTraceHidden is applied to the method.
362+
return false;
363+
}
362364

363-
Type? declaringType = mb.DeclaringType;
364-
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
365-
if (declaringType != null &&
366-
declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
365+
Type? declaringType = mb.DeclaringType;
366+
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
367+
if (declaringType != null &&
368+
declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
369+
{
370+
// Don't show where StackTraceHidden is applied to the containing Type of the method.
371+
return false;
372+
}
373+
}
374+
catch
367375
{
368-
// Don't show where StackTraceHidden is applied to the containing Type of the method.
369-
return false;
376+
// Getting the StackTraceHiddenAttribute has failed, behave as if it was not present.
377+
// One of the reasons can be that the method mb or its declaring type use attributes
378+
// defined in an assembly that is missing.
370379
}
371380

372381
return true;

0 commit comments

Comments
 (0)