From fcb504859c515aaffa0ba62e031707d91824d8b0 Mon Sep 17 00:00:00 2001 From: Ilshat Mukhamedyarov Date: Fri, 9 Aug 2024 09:51:21 +0300 Subject: [PATCH 1/3] exception handling template --- ...Mindbox.ExceptionsHandling.Template.csproj | 15 +++++++++ .../SentryExceptionsCategoryProcessor.cs | 22 +++++++++++++ .../SentryOptions.cs | 10 ++++++ .../ServiceCollectionExtensions.cs | 32 +++++++++++++++++++ Mindbox.ExceptionsHandling.sln | 6 ++++ 5 files changed, 85 insertions(+) create mode 100644 Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj create mode 100644 Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs create mode 100644 Mindbox.ExceptionsHandling.Template/SentryOptions.cs create mode 100644 Mindbox.ExceptionsHandling.Template/ServiceCollectionExtensions.cs diff --git a/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj new file mode 100644 index 0000000..b8e9ff9 --- /dev/null +++ b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj @@ -0,0 +1,15 @@ + + + net8.0 + Mindbox.ExceptionsHandling.Template + Mindbox.ExceptionsHandling.Template + true + enable + + + + + + + + diff --git a/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs b/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs new file mode 100644 index 0000000..6383aea --- /dev/null +++ b/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Options; +using Sentry; +using Sentry.Extensibility; + +namespace Mindbox.ExceptionsHandling.Template; + +public class SentryExceptionsCategoryProcessor( + IExceptionCategoryMatcher exceptionCategoryMatcher, + IOptions sentryOptions) + : ISentryEventProcessor +{ + public SentryEvent? Process(SentryEvent @event) + { + if (@event.Exception != null) + { + var category = exceptionCategoryMatcher.TryGetTopCategory(@event.Exception); + if (category != null && category.LogLevel < sentryOptions.Value.MinimumEventLevel) return null; + } + + return @event; + } +} \ No newline at end of file diff --git a/Mindbox.ExceptionsHandling.Template/SentryOptions.cs b/Mindbox.ExceptionsHandling.Template/SentryOptions.cs new file mode 100644 index 0000000..fd11acd --- /dev/null +++ b/Mindbox.ExceptionsHandling.Template/SentryOptions.cs @@ -0,0 +1,10 @@ +using Microsoft.Extensions.Logging; + +namespace Mindbox.ExceptionsHandling.Template; + +public record SentryOptions +{ + public const string SectionName = "Sentry"; + + public LogLevel MinimumEventLevel { get; set; } = LogLevel.Error; +} \ No newline at end of file diff --git a/Mindbox.ExceptionsHandling.Template/ServiceCollectionExtensions.cs b/Mindbox.ExceptionsHandling.Template/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..d711075 --- /dev/null +++ b/Mindbox.ExceptionsHandling.Template/ServiceCollectionExtensions.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Mindbox.ExceptionsHandling.Template; + +public static class ServiceCollectionExtensions +{ + /// + /// Добавляет фильтрацию sentry ивентов по . + /// Ивент в sentry не будет уходить, + /// если его уровень ниже чем указанный уровень в . + /// + /// + /// проставляется в конфигах в + /// секции . + /// Если в конфигах не проставлен , + /// то будет использоваться уровень . + /// + public static IServiceCollection AddSentryExceptionsCategoryProcessor( + this IServiceCollection services, + IConfiguration configuration) + { + services + .AddOptions() + .Bind(configuration.GetSection(SentryOptions.SectionName)); + + services.AddSingleton(); + + return services; + } +} \ No newline at end of file diff --git a/Mindbox.ExceptionsHandling.sln b/Mindbox.ExceptionsHandling.sln index 3643b12..5593e03 100644 --- a/Mindbox.ExceptionsHandling.sln +++ b/Mindbox.ExceptionsHandling.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Directory.Build.props = Directory.Build.props EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mindbox.ExceptionsHandling.Template", "Mindbox.ExceptionsHandling.Template\Mindbox.ExceptionsHandling.Template.csproj", "{F7E5AE5C-E4F1-4F96-BDE9-71B66EE5B811}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,5 +37,9 @@ Global {7BE30239-3C77-4BF8-A8D0-E55C1A87A5A7}.Debug|Any CPU.Build.0 = Debug|Any CPU {7BE30239-3C77-4BF8-A8D0-E55C1A87A5A7}.Release|Any CPU.ActiveCfg = Release|Any CPU {7BE30239-3C77-4BF8-A8D0-E55C1A87A5A7}.Release|Any CPU.Build.0 = Release|Any CPU + {F7E5AE5C-E4F1-4F96-BDE9-71B66EE5B811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F7E5AE5C-E4F1-4F96-BDE9-71B66EE5B811}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F7E5AE5C-E4F1-4F96-BDE9-71B66EE5B811}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F7E5AE5C-E4F1-4F96-BDE9-71B66EE5B811}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 588931814c639b71af7e9f7d628adcb1807e6a2f Mon Sep 17 00:00:00 2001 From: Ilshat Mukhamedyarov Date: Fri, 9 Aug 2024 09:56:22 +0300 Subject: [PATCH 2/3] Update Mindbox.ExceptionsHandling.Template.csproj --- .../Mindbox.ExceptionsHandling.Template.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj index b8e9ff9..08f4813 100644 --- a/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj +++ b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj @@ -1,6 +1,6 @@  - net8.0 + netstandard2.0 Mindbox.ExceptionsHandling.Template Mindbox.ExceptionsHandling.Template true From f673bb7747f1e8288fbe7e4a0e5403a83cc23820 Mon Sep 17 00:00:00 2001 From: Ilshat Mukhamedyarov Date: Fri, 9 Aug 2024 10:02:19 +0300 Subject: [PATCH 3/3] * --- ...Mindbox.ExceptionsHandling.Template.csproj | 1 - .../SentryExceptionsCategoryProcessor.cs | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj index 08f4813..4a5eae5 100644 --- a/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj +++ b/Mindbox.ExceptionsHandling.Template/Mindbox.ExceptionsHandling.Template.csproj @@ -3,7 +3,6 @@ netstandard2.0 Mindbox.ExceptionsHandling.Template Mindbox.ExceptionsHandling.Template - true enable diff --git a/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs b/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs index 6383aea..7d48024 100644 --- a/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs +++ b/Mindbox.ExceptionsHandling.Template/SentryExceptionsCategoryProcessor.cs @@ -4,17 +4,26 @@ namespace Mindbox.ExceptionsHandling.Template; -public class SentryExceptionsCategoryProcessor( - IExceptionCategoryMatcher exceptionCategoryMatcher, - IOptions sentryOptions) +public class SentryExceptionsCategoryProcessor : ISentryEventProcessor { + private readonly IExceptionCategoryMatcher _exceptionCategoryMatcher; + private readonly IOptions _sentryOptions; + + public SentryExceptionsCategoryProcessor( + IExceptionCategoryMatcher exceptionCategoryMatcher, + IOptions sentryOptions) + { + _exceptionCategoryMatcher = exceptionCategoryMatcher; + _sentryOptions = sentryOptions; + } + public SentryEvent? Process(SentryEvent @event) { if (@event.Exception != null) { - var category = exceptionCategoryMatcher.TryGetTopCategory(@event.Exception); - if (category != null && category.LogLevel < sentryOptions.Value.MinimumEventLevel) return null; + var category = _exceptionCategoryMatcher.TryGetTopCategory(@event.Exception); + if (category != null && category.LogLevel < _sentryOptions.Value.MinimumEventLevel) return null; } return @event;