From fd210f24041ef566b75e36abb137703eaaa1e5cd Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Sat, 1 Jul 2023 19:00:38 +0200 Subject: [PATCH] Added new sentry support --- Moonlight/Moonlight.csproj | 2 + Moonlight/Program.cs | 87 +++++++++++++++++--- Moonlight/defaultstorage/configs/config.json | 4 + 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index c4a6dca..dec7a17 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -46,6 +46,8 @@ + + diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 697ccdd..d706fd9 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -29,7 +29,9 @@ using Moonlight.App.Services.Notifications; using Moonlight.App.Services.Sessions; using Moonlight.App.Services.Statistics; using Moonlight.App.Services.SupportChat; +using Sentry; using Serilog; +using Serilog.Events; using Serilog.Sinks.SystemConsole.Themes; namespace Moonlight @@ -40,24 +42,62 @@ namespace Moonlight { // This will also copy all default config files var configService = new ConfigService(new StorageService()); + var shouldUseSentry = configService + .GetSection("Moonlight") + .GetSection("Sentry") + .GetValue("Enable"); if (configService.DebugMode) { - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Verbose() - .Enrich.FromLogContext() - .WriteTo.Console( - outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") - .CreateLogger(); + if (shouldUseSentry) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Verbose() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .WriteTo.Sentry(options => + { + options.MinimumBreadcrumbLevel = LogEventLevel.Debug; + options.MinimumEventLevel = LogEventLevel.Warning; + }) + .CreateLogger(); + } + else + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Verbose() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + } } else { - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Information() - .Enrich.FromLogContext() - .WriteTo.Console( - outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") - .CreateLogger(); + if (shouldUseSentry) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .WriteTo.Sentry(options => + { + options.MinimumBreadcrumbLevel = LogEventLevel.Information; + options.MinimumEventLevel = LogEventLevel.Warning; + }) + .CreateLogger(); + } + else + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + } } Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}"); @@ -66,7 +106,7 @@ namespace Moonlight var databaseCheckupService = new DatabaseCheckupService(configService); await databaseCheckupService.Perform(); - + var builder = WebApplication.CreateBuilder(args); // Switch to logging.net injection @@ -88,6 +128,21 @@ namespace Moonlight .AddCheck("Database") .AddCheck("Nodes") .AddCheck("Daemons"); + + // Sentry + if (shouldUseSentry) + { + builder.WebHost.UseSentry(options => + { + options.Dsn = configService + .GetSection("Moonlight") + .GetSection("Sentry") + .GetValue("Dsn"); + + options.Debug = configService.DebugMode; + options.DiagnosticLevel = SentryLevel.Warning; + }); + } // Databases builder.Services.AddDbContext(); @@ -203,6 +258,12 @@ namespace Moonlight app.UseHsts(); } + // Sentry + if (shouldUseSentry) + { + app.UseSentryTracing(); + } + app.UseStaticFiles(); app.UseRouting(); app.UseWebSockets(); diff --git a/Moonlight/defaultstorage/configs/config.json b/Moonlight/defaultstorage/configs/config.json index bc1bc39..ade2ab5 100644 --- a/Moonlight/defaultstorage/configs/config.json +++ b/Moonlight/defaultstorage/configs/config.json @@ -92,6 +92,10 @@ "Url": "link-to-google.page", "MinRating": 4, "DaysSince": 5 + }, + "Sentry": { + "Enable": false, + "Dsn": "" } } } \ No newline at end of file