From 578cb793e382573cb04e33ad3f68f78e6bc96e86 Mon Sep 17 00:00:00 2001 From: Pavel Steinl Date: Wed, 9 Feb 2022 12:52:55 +0100 Subject: [PATCH] Add RegexMatchTimeoutExceptionDestructurer for RegexMatchTimeoutException --- .../Core/DestructuringOptionsBuilder.cs | 1 + .../RegexMatchTimeoutExceptionDestructurer.cs | 33 +++++++++++++++++++ ...xMatchTimeoutExceptionDestructurerTests.cs | 30 +++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Source/Serilog.Exceptions/Destructurers/RegexMatchTimeoutExceptionDestructurer.cs create mode 100644 Tests/Serilog.Exceptions.Test/Destructurers/RegexMatchTimeoutExceptionDestructurerTests.cs diff --git a/Source/Serilog.Exceptions/Core/DestructuringOptionsBuilder.cs b/Source/Serilog.Exceptions/Core/DestructuringOptionsBuilder.cs index 12460026..b0b76f57 100644 --- a/Source/Serilog.Exceptions/Core/DestructuringOptionsBuilder.cs +++ b/Source/Serilog.Exceptions/Core/DestructuringOptionsBuilder.cs @@ -21,6 +21,7 @@ public class DestructuringOptionsBuilder : IDestructuringOptions new ArgumentExceptionDestructurer(), new ArgumentOutOfRangeExceptionDestructurer(), new AggregateExceptionDestructurer(), + new RegexMatchTimeoutExceptionDestructurer(), new ReflectionTypeLoadExceptionDestructurer(), new OperationCanceledExceptionDestructurer(), new TaskCanceledExceptionDestructurer(), diff --git a/Source/Serilog.Exceptions/Destructurers/RegexMatchTimeoutExceptionDestructurer.cs b/Source/Serilog.Exceptions/Destructurers/RegexMatchTimeoutExceptionDestructurer.cs new file mode 100644 index 00000000..dd2cd9a1 --- /dev/null +++ b/Source/Serilog.Exceptions/Destructurers/RegexMatchTimeoutExceptionDestructurer.cs @@ -0,0 +1,33 @@ +namespace Serilog.Exceptions.Destructurers +{ + using System; + using System.Collections.Generic; + using System.Text.RegularExpressions; + using Serilog.Exceptions.Core; + + /// + /// Destructurer for . + /// + public class RegexMatchTimeoutExceptionDestructurer + : ExceptionDestructurer + { + /// + public override Type[] TargetTypes => new Type[] + { + typeof(RegexMatchTimeoutException), + }; + + /// + public override void Destructure(Exception exception, IExceptionPropertiesBag propertiesBag, Func?> destructureException) + { + base.Destructure(exception, propertiesBag, destructureException); + +#pragma warning disable CA1062 // Validate arguments of public methods + var typedException = (RegexMatchTimeoutException)exception; + propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.Input), typedException.Input); + propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.Pattern), typedException.Pattern); + propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.MatchTimeout), typedException.MatchTimeout.ToString("c")); +#pragma warning restore CA1062 // Validate arguments of public methods + } + } +} diff --git a/Tests/Serilog.Exceptions.Test/Destructurers/RegexMatchTimeoutExceptionDestructurerTests.cs b/Tests/Serilog.Exceptions.Test/Destructurers/RegexMatchTimeoutExceptionDestructurerTests.cs new file mode 100644 index 00000000..7744f23f --- /dev/null +++ b/Tests/Serilog.Exceptions.Test/Destructurers/RegexMatchTimeoutExceptionDestructurerTests.cs @@ -0,0 +1,30 @@ +namespace Serilog.Exceptions.Test.Destructurers +{ + using System; + using System.Text.RegularExpressions; + using Serilog.Exceptions.Core; + using Serilog.Exceptions.Destructurers; + using Xunit; + using static LogJsonOutputUtils; + + public class RegexMatchTimeoutExceptionDestructurerTests + { + [Fact] + public void RegexMatchTimeoutException_ParamsAttachedAsProperties() + { + var exception = new RegexMatchTimeoutException("input", "pattern", TimeSpan.FromSeconds(1)); + + var optionsBuilder = new DestructuringOptionsBuilder() + .WithDestructurers(new IExceptionDestructurer[] + { + new RegexMatchTimeoutExceptionDestructurer(), + }); + + var loggedExceptionDetails = ExtractExceptionDetails(LogAndDestructureException(exception, optionsBuilder)); + + Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.Input), exception.Input); + Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.Pattern), exception.Pattern); + Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.MatchTimeout), exception.MatchTimeout.ToString("c")); + } + } +}