Merge pull request #202 from Moonlight-Panel/AddSentrySupport

Added new sentry support
This commit is contained in:
Marcel Baumgartner
2023-07-01 19:01:00 +02:00
committed by GitHub
3 changed files with 80 additions and 13 deletions

View File

@@ -46,6 +46,8 @@
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
<PackageReference Include="QRCoder" Version="1.4.3" /> <PackageReference Include="QRCoder" Version="1.4.3" />
<PackageReference Include="RestSharp" Version="109.0.0-preview.1" /> <PackageReference Include="RestSharp" Version="109.0.0-preview.1" />
<PackageReference Include="Sentry.AspNetCore" Version="3.33.1" />
<PackageReference Include="Sentry.Serilog" Version="3.33.1" />
<PackageReference Include="Serilog" Version="3.0.0" /> <PackageReference Include="Serilog" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.1-dev-00910" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.1.1-dev-00910" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00947" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00947" />

View File

@@ -29,7 +29,9 @@ using Moonlight.App.Services.Notifications;
using Moonlight.App.Services.Sessions; using Moonlight.App.Services.Sessions;
using Moonlight.App.Services.Statistics; using Moonlight.App.Services.Statistics;
using Moonlight.App.Services.SupportChat; using Moonlight.App.Services.SupportChat;
using Sentry;
using Serilog; using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes; using Serilog.Sinks.SystemConsole.Themes;
namespace Moonlight namespace Moonlight
@@ -40,14 +42,51 @@ namespace Moonlight
{ {
// This will also copy all default config files // This will also copy all default config files
var configService = new ConfigService(new StorageService()); var configService = new ConfigService(new StorageService());
var shouldUseSentry = configService
.GetSection("Moonlight")
.GetSection("Sentry")
.GetValue<bool>("Enable");
if (configService.DebugMode) if (configService.DebugMode)
{
if (shouldUseSentry)
{ {
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose() .MinimumLevel.Verbose()
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Console( .WriteTo.Console(
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") 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
{
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(); .CreateLogger();
} }
else else
@@ -59,6 +98,7 @@ namespace Moonlight
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
.CreateLogger(); .CreateLogger();
} }
}
Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}"); Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}");
@@ -89,6 +129,21 @@ namespace Moonlight
.AddCheck<NodeHealthCheck>("Nodes") .AddCheck<NodeHealthCheck>("Nodes")
.AddCheck<DaemonHealthCheck>("Daemons"); .AddCheck<DaemonHealthCheck>("Daemons");
// Sentry
if (shouldUseSentry)
{
builder.WebHost.UseSentry(options =>
{
options.Dsn = configService
.GetSection("Moonlight")
.GetSection("Sentry")
.GetValue<string>("Dsn");
options.Debug = configService.DebugMode;
options.DiagnosticLevel = SentryLevel.Warning;
});
}
// Databases // Databases
builder.Services.AddDbContext<DataContext>(); builder.Services.AddDbContext<DataContext>();
@@ -203,6 +258,12 @@ namespace Moonlight
app.UseHsts(); app.UseHsts();
} }
// Sentry
if (shouldUseSentry)
{
app.UseSentryTracing();
}
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseWebSockets(); app.UseWebSockets();

View File

@@ -92,6 +92,10 @@
"Url": "link-to-google.page", "Url": "link-to-google.page",
"MinRating": 4, "MinRating": 4,
"DaysSince": 5 "DaysSince": 5
},
"Sentry": {
"Enable": false,
"Dsn": ""
} }
} }
} }