Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception handling template #33

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Mindbox.ExceptionsHandling.Template</RootNamespace>
<AssemblyName>Mindbox.ExceptionsHandling.Template</AssemblyName>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Mindbox.ExceptionsHandling.Abstractions\Mindbox.ExceptionsHandling.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Sentry.Extensions.Logging" Version="4.8.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Options;
using Sentry;
using Sentry.Extensibility;

namespace Mindbox.ExceptionsHandling.Template;

public class SentryExceptionsCategoryProcessor
: ISentryEventProcessor
{
private readonly IExceptionCategoryMatcher _exceptionCategoryMatcher;
private readonly IOptions<SentryOptions> _sentryOptions;

public SentryExceptionsCategoryProcessor(
IExceptionCategoryMatcher exceptionCategoryMatcher,
IOptions<SentryOptions> 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;
}

return @event;
}
}
10 changes: 10 additions & 0 deletions Mindbox.ExceptionsHandling.Template/SentryOptions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 32 additions & 0 deletions Mindbox.ExceptionsHandling.Template/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Mindbox.ExceptionsHandling.Template;

public static class ServiceCollectionExtensions
{
/// <summary>
/// Добавляет фильтрацию sentry ивентов по <see cref="IExceptionCategory.LogLevel" />.
/// Ивент в sentry не будет уходить,
/// если его уровень ниже чем указанный уровень в <see cref="SentryOptions.MinimumEventLevel" />.
/// </summary>
/// <remarks>
/// <see cref="SentryOptions.MinimumEventLevel" /> проставляется в конфигах в
/// секции <see cref="SentryOptions.SectionName" />.
/// Если в конфигах не проставлен <see cref="SentryOptions.MinimumEventLevel" />,
/// то будет использоваться уровень <see cref="LogLevel.Error" />.
/// </remarks>
public static IServiceCollection AddSentryExceptionsCategoryProcessor(
this IServiceCollection services,
IConfiguration configuration)
{
services
.AddOptions<SentryOptions>()
.Bind(configuration.GetSection(SentryOptions.SectionName));

services.AddSingleton<SentryExceptionsCategoryProcessor>();

return services;
}
}
10 changes: 10 additions & 0 deletions Mindbox.ExceptionsHandling.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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
Expand All @@ -29,5 +31,13 @@ Global
{192FA40C-6379-4CFB-AE0A-99DF91C5D367}.Debug|Any CPU.Build.0 = Debug|Any CPU
{192FA40C-6379-4CFB-AE0A-99DF91C5D367}.Release|Any CPU.ActiveCfg = Release|Any CPU
{192FA40C-6379-4CFB-AE0A-99DF91C5D367}.Release|Any CPU.Build.0 = Release|Any CPU
{7BE30239-3C77-4BF8-A8D0-E55C1A87A5A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
Loading