Skip to content

Commit

Permalink
Make Exception.StackTrace resilient to missing attribute assemblies (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
janvorli authored Feb 24, 2021
1 parent e912151 commit cbb5b90
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,28 @@ private static bool ShowInStackTrace(MethodBase mb)
return false;
}

if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
try
{
// Don't show where StackTraceHidden is applied to the method.
return false;
}
if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the method.
return false;
}

Type? declaringType = mb.DeclaringType;
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
if (declaringType != null &&
declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
Type? declaringType = mb.DeclaringType;
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
if (declaringType != null &&
declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the containing Type of the method.
return false;
}
}
catch
{
// Don't show where StackTraceHidden is applied to the containing Type of the method.
return false;
// Getting the StackTraceHiddenAttribute has failed, behave as if it was not present.
// One of the reasons can be that the method mb or its declaring type use attributes
// defined in an assembly that is missing.
}

return true;
Expand Down

0 comments on commit cbb5b90

Please sign in to comment.