diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization/Implementation/ExceptionSummarizer.cs b/src/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization/Implementation/ExceptionSummarizer.cs index 869467f81cc..4ef0ea261ff 100644 --- a/src/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization/Implementation/ExceptionSummarizer.cs +++ b/src/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization/Implementation/ExceptionSummarizer.cs @@ -9,18 +9,11 @@ namespace Microsoft.Extensions.Diagnostics.ExceptionSummarization; -/// -/// Looks through all the registered summary providers, returns a summary if possible. -/// internal sealed class ExceptionSummarizer : IExceptionSummarizer { private const string DefaultDescription = "Unknown"; private readonly FrozenDictionary _exceptionTypesToProviders; - /// - /// Initializes a new instance of the class. - /// - /// All registered exception providers. public ExceptionSummarizer(IEnumerable providers) { var exceptionTypesToProvidersBuilder = new Dictionary(); @@ -35,21 +28,22 @@ public ExceptionSummarizer(IEnumerable providers) _exceptionTypesToProviders = exceptionTypesToProvidersBuilder.ToFrozenDictionary(); } - /// - /// It iterates through all registered summarizers, returns a summary if possible. - /// Default is message if its length is less than 32, otherwise exception type name. - /// - /// The exception. - /// The summary of the given . public ExceptionSummary Summarize(Exception exception) { _ = Throw.IfNull(exception); var exceptionType = exception.GetType(); var exceptionTypeName = exception.GetType().Name; - if (_exceptionTypesToProviders.TryGetValue(exceptionType, out var exceptionSummaryProvider)) + // find a match for the exception type or a base type thereof + var type = exceptionType; + while (type != null) { - return BuildSummary(exception, exceptionSummaryProvider, exceptionTypeName); + if (_exceptionTypesToProviders.TryGetValue(type, out var exceptionSummaryProvider)) + { + return BuildSummary(exception, exceptionSummaryProvider, exceptionTypeName); + } + + type = type.BaseType; } // Let's see if we get lucky with the inner exception diff --git a/test/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization.Tests/Implementation/ExceptionSummarizerTests.cs b/test/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization.Tests/Implementation/ExceptionSummarizerTests.cs index f2a36086aa6..fbd1cfb2e5c 100644 --- a/test/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization.Tests/Implementation/ExceptionSummarizerTests.cs +++ b/test/Libraries/Microsoft.Extensions.Diagnostics.ExceptionSummarization.Tests/Implementation/ExceptionSummarizerTests.cs @@ -294,4 +294,24 @@ public void Summarize_WithDefaultHResultAndInnerException_ReturnSummary() Assert.Equal(exceptionHResultSummary, summary); Assert.Equal(exceptionHResultSummary.ToString(), summary.ToString()); } + + private class DerivedException : TestException + { + public DerivedException(uint hResult, string message, Exception innerException) + : base(hResult, message, innerException) + { + } + } + + [Fact] + public void Summarize_WithDerivedExceptionType_ReturnSummary() + { + var exception = new DerivedException(0, "Test", new TestException(0)); + var exceptionHResultSummary = new ExceptionSummary("DerivedException", "TestException", "Unknown"); + + var summary = _exceptionSummarizer.Summarize(exception); + + Assert.Equal(exceptionHResultSummary, summary); + Assert.Equal(exceptionHResultSummary.ToString(), summary.ToString()); + } }