From 54471a1128b4897ecdbd01cb384ceacef6b13165 Mon Sep 17 00:00:00 2001 From: meysamhadeli Date: Mon, 2 Jan 2023 21:51:53 +0330 Subject: [PATCH] feat: Add serilog sentry --- .github/workflows/release-please.yml | 15 +++++++++++++++ src/BuildingBlocks/BuildingBlocks.csproj | 2 ++ src/BuildingBlocks/Logging/Extensions.cs | 19 +++++++++++++++++++ src/BuildingBlocks/Logging/LogOptions.cs | 4 +++- src/BuildingBlocks/Logging/SentryOptions.cs | 9 +++++++++ .../Swagger/ServiceCollectionExtensions.cs | 12 +++++++++--- .../Booking/src/Booking.Api/appsettings.json | 6 ++++++ .../Flight/src/Flight.Api/appsettings.json | 6 ++++++ .../src/Identity.Api/appsettings.json | 6 ++++++ .../src/Passenger.Api/appsettings.json | 6 ++++++ 10 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/release-please.yml create mode 100644 src/BuildingBlocks/Logging/SentryOptions.cs diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml new file mode 100644 index 0000000..bd31072 --- /dev/null +++ b/.github/workflows/release-please.yml @@ -0,0 +1,15 @@ +# https://github.com/google-github-actions/release-please-action#how-release-please-works +# https://www.conventionalcommits.org/en/v1.0.0/ +on: + push: + branches: + - main +name: release-please +jobs: + release-please: + runs-on: ubuntu-latest + steps: + - uses: google-github-actions/release-please-action@v3 + with: + release-type: go + package-name: release-please-action diff --git a/src/BuildingBlocks/BuildingBlocks.csproj b/src/BuildingBlocks/BuildingBlocks.csproj index c1e530e..736db5c 100644 --- a/src/BuildingBlocks/BuildingBlocks.csproj +++ b/src/BuildingBlocks/BuildingBlocks.csproj @@ -66,6 +66,7 @@ + @@ -100,6 +101,7 @@ + diff --git a/src/BuildingBlocks/Logging/Extensions.cs b/src/BuildingBlocks/Logging/Extensions.cs index 825b70a..c47a47b 100644 --- a/src/BuildingBlocks/Logging/Extensions.cs +++ b/src/BuildingBlocks/Logging/Extensions.cs @@ -48,6 +48,25 @@ namespace BuildingBlocks.Logging }); } + + if (logOptions?.Sentry is {Enable: true}) + { + var minimumBreadcrumbLevel = Enum.TryParse(logOptions.Level, true, out var minBreadcrumbLevel) + ? minBreadcrumbLevel + : LogEventLevel.Information; + + var minimumEventLevel = Enum.TryParse(logOptions.Sentry.MinimumEventLevel, true, out var minEventLevel) + ? minEventLevel + : LogEventLevel.Error; + + loggerConfiguration.WriteTo.Sentry(o => + { + o.Dsn = logOptions.Sentry.Dsn; + o.MinimumBreadcrumbLevel = minimumBreadcrumbLevel; + o.MinimumEventLevel = minimumEventLevel; + }); + } + if (logOptions.File is { Enable: true }) { var root = env.ContentRootPath; diff --git a/src/BuildingBlocks/Logging/LogOptions.cs b/src/BuildingBlocks/Logging/LogOptions.cs index 763ae45..c7d880d 100644 --- a/src/BuildingBlocks/Logging/LogOptions.cs +++ b/src/BuildingBlocks/Logging/LogOptions.cs @@ -4,7 +4,9 @@ { public string Level { get; set; } public ElasticOptions Elastic { get; set; } + + public SentryOptions Sentry { get; set; } public FileOptions File { get; set; } public string LogTemplate { get; set; } } -} \ No newline at end of file +} diff --git a/src/BuildingBlocks/Logging/SentryOptions.cs b/src/BuildingBlocks/Logging/SentryOptions.cs new file mode 100644 index 0000000..6eb4b4e --- /dev/null +++ b/src/BuildingBlocks/Logging/SentryOptions.cs @@ -0,0 +1,9 @@ +namespace BuildingBlocks.Logging; + +public class SentryOptions +{ + public bool Enable { get; set; } + public string Dsn { get; set; } + public string MinimumBreadcrumbLevel { get; set; } + public string MinimumEventLevel { get; set; } +} diff --git a/src/BuildingBlocks/Swagger/ServiceCollectionExtensions.cs b/src/BuildingBlocks/Swagger/ServiceCollectionExtensions.cs index f032b99..cc96b21 100644 --- a/src/BuildingBlocks/Swagger/ServiceCollectionExtensions.cs +++ b/src/BuildingBlocks/Swagger/ServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; +using Unchase.Swashbuckle.AspNetCore.Extensions.Extensions; namespace BuildingBlocks.Swagger; @@ -17,7 +18,7 @@ public static class ServiceCollectionExtensions // https://github.com/dotnet/aspnet-api-versioning/tree/88323136a97a59fcee24517a514c1a445530c7e2/examples/AspNetCore/WebApi/MinimalOpenApiExample public static IServiceCollection AddCustomSwagger(this IServiceCollection services, IConfiguration configuration, - Assembly assembly) + params Assembly[] assemblies) { // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/openapi services.AddEndpointsApiExplorer(); @@ -31,9 +32,14 @@ public static class ServiceCollectionExtensions { options.OperationFilter(); - var xmlFile = XmlCommentsFilePath(assembly); - if (File.Exists(xmlFile)) options.IncludeXmlComments(xmlFile); + foreach (var assembly in assemblies) + { + var xmlFile = XmlCommentsFilePath(assembly); + if (File.Exists(xmlFile)) options.IncludeXmlComments(xmlFile); + } + + options.AddEnumsWithValuesFixFilters(); options.AddSecurityRequirement(new OpenApiSecurityRequirement { diff --git a/src/Services/Booking/src/Booking.Api/appsettings.json b/src/Services/Booking/src/Booking.Api/appsettings.json index a794e5e..bb84c2d 100644 --- a/src/Services/Booking/src/Booking.Api/appsettings.json +++ b/src/Services/Booking/src/Booking.Api/appsettings.json @@ -13,6 +13,12 @@ "enable": false, "path": "logs/logs.txt", "interval": "day" + }, + "Sentry": { + "enable": false, + "dsn": "", + "minimumBreadcrumbLevel": "information", + "minimumEventLevel":"error" } }, "Jwt": { diff --git a/src/Services/Flight/src/Flight.Api/appsettings.json b/src/Services/Flight/src/Flight.Api/appsettings.json index 4b77b07..be45ae2 100644 --- a/src/Services/Flight/src/Flight.Api/appsettings.json +++ b/src/Services/Flight/src/Flight.Api/appsettings.json @@ -13,6 +13,12 @@ "enable": false, "path": "logs/logs.txt", "interval": "day" + }, + "Sentry": { + "enable": false, + "dsn": "", + "minimumBreadcrumbLevel": "information", + "minimumEventLevel":"error" } }, "DatabaseOptions": { diff --git a/src/Services/Identity/src/Identity.Api/appsettings.json b/src/Services/Identity/src/Identity.Api/appsettings.json index b76b8e3..2ffcf3c 100644 --- a/src/Services/Identity/src/Identity.Api/appsettings.json +++ b/src/Services/Identity/src/Identity.Api/appsettings.json @@ -27,6 +27,12 @@ "enable": false, "path": "logs/logs.txt", "interval": "day" + }, + "Sentry": { + "enable": false, + "dsn": "", + "minimumBreadcrumbLevel": "information", + "minimumEventLevel":"error" } }, "PersistMessageOptions": { diff --git a/src/Services/Passenger/src/Passenger.Api/appsettings.json b/src/Services/Passenger/src/Passenger.Api/appsettings.json index d7cbb30..c26cda4 100644 --- a/src/Services/Passenger/src/Passenger.Api/appsettings.json +++ b/src/Services/Passenger/src/Passenger.Api/appsettings.json @@ -31,6 +31,12 @@ "enable": false, "path": "logs/logs.txt", "interval": "day" + }, + "Sentry": { + "enable": false, + "dsn": "", + "minimumBreadcrumbLevel": "information", + "minimumEventLevel":"error" } }, "PersistMessageOptions": {