Compare commits

...

3 Commits

4 changed files with 101 additions and 25 deletions

View File

@ -0,0 +1,22 @@
using OF_DL.Services;
using Serilog;
namespace OF_DL.Helpers;
public class ExitHelper(IDownloadEventHandler eventHandler)
{
private readonly IDownloadEventHandler _eventHandler = eventHandler;
public void ExitWithCode(int exitCode)
{
Console.WriteLine();
_eventHandler?.OnMessage("Exiting run with Code '{exitCode}'..");
Log.CloseAndFlush();
Log.CloseAndFlush();
Task.Delay(5000).GetAwaiter().GetResult();
Environment.Exit(exitCode);
}
}

View File

@ -19,6 +19,7 @@
<PackageReference Include="Serilog" Version="4.3.1"/> <PackageReference Include="Serilog" Version="4.3.1"/>
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1"/> <PackageReference Include="Serilog.Sinks.Console" Version="6.1.1"/>
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0"/> <PackageReference Include="Serilog.Sinks.File" Version="7.0.0"/>
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.0"/>
<PackageReference Include="System.Reactive" Version="6.1.0"/> <PackageReference Include="System.Reactive" Version="6.1.0"/>
<PackageReference Include="xFFmpeg.NET" Version="7.2.0"/> <PackageReference Include="xFFmpeg.NET" Version="7.2.0"/>
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,50 @@
using OF_DL.Enumerations;
using Serilog;
using Serilog.Core;
using Serilog.Events;
namespace OF_DL.Services;
public class SeqLoggingService : ILoggingService
{
public SeqLoggingService()
{
LevelSwitch = new LoggingLevelSwitch();
InitializeLoggerWithSeq();
}
/// <summary>
/// Gets the level switch that controls runtime logging verbosity.
/// </summary>
public LoggingLevelSwitch LevelSwitch { get; }
/// <summary>
/// Updates the minimum logging level at runtime.
/// </summary>
/// <param name="newLevel">The new minimum log level.</param>
public void UpdateLoggingLevel(LoggingLevel newLevel)
{
LevelSwitch.MinimumLevel = (LogEventLevel)newLevel;
Log.Debug("Logging level updated to: {LoggingLevel}", newLevel);
}
/// <summary>
/// Returns the current minimum logging level.
/// </summary>
public LoggingLevel GetCurrentLoggingLevel() => (LoggingLevel)LevelSwitch.MinimumLevel;
private void InitializeLoggerWithSeq()
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "OF_DL")
.Enrich.WithProperty("StartTime", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ")
.Enrich.WithProperty("MachineName", Environment.MachineName)
.MinimumLevel.ControlledBy(LevelSwitch)
.WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error)
.WriteTo.Seq("https://seq.cajetan.dk")
.CreateLogger();
Log.Debug("Logging service initialized");
}
}

View File

@ -9,6 +9,7 @@ using OF_DL.Models.Entities.Users;
using OF_DL.Services; using OF_DL.Services;
using Serilog; using Serilog;
using Spectre.Console; using Spectre.Console;
using OF_DL.Helpers;
namespace OF_DL; namespace OF_DL;
@ -59,7 +60,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine("[red]Press any key to exit.[/]"); AnsiConsole.MarkupLine("[red]Press any key to exit.[/]");
Log.Error("auth invalid after attempt to get auth from browser"); Log.Error("auth invalid after attempt to get auth from browser");
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
await authService.SaveToFileAsync(); await authService.SaveToFileAsync();
@ -83,13 +84,14 @@ public class Program(IServiceProvider serviceProvider)
{ {
// Set up dependency injection with LoggingService and ConfigService // Set up dependency injection with LoggingService and ConfigService
ServiceCollection services = new(); ServiceCollection services = new();
services.AddSingleton<ILoggingService, LoggingService>(); services.AddSingleton<ILoggingService, SeqLoggingService>();
services.AddSingleton<IConfigService, ConfigService>(); services.AddSingleton<IConfigService, ConfigService>();
services.AddSingleton<ExitHelper>();
ServiceProvider tempServiceProvider = services.BuildServiceProvider(); ServiceProvider tempServiceProvider = services.BuildServiceProvider();
ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>(); ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>();
IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>(); IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>();
ExitHelper tempExitHelper = tempServiceProvider.GetRequiredService<ExitHelper>();
if (!await configService.LoadConfigurationAsync(args)) if (!await configService.LoadConfigurationAsync(args))
{ {
@ -100,7 +102,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(3); tempExitHelper.ExitWithCode(3);
} }
AnsiConsole.Markup("[green]config.conf located successfully!\n[/]"); AnsiConsole.Markup("[green]config.conf located successfully!\n[/]");
@ -117,6 +119,7 @@ public class Program(IServiceProvider serviceProvider)
services.AddSingleton<IStartupService, StartupService>(); services.AddSingleton<IStartupService, StartupService>();
services.AddSingleton<IDownloadOrchestrationService, DownloadOrchestrationService>(); services.AddSingleton<IDownloadOrchestrationService, DownloadOrchestrationService>();
services.AddSingleton<Program>(); services.AddSingleton<Program>();
services.AddSingleton<ExitHelper>();
return services; return services;
} }
@ -152,7 +155,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(1); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(1);
} }
if (!startupResult.FfmpegFound) if (!startupResult.FfmpegFound)
@ -169,7 +172,7 @@ public class Program(IServiceProvider serviceProvider)
"[red]Cannot locate FFmpeg; please modify config.conf with the correct path.[/]"); "[red]Cannot locate FFmpeg; please modify config.conf with the correct path.[/]");
} }
Environment.Exit(4); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(4);
} }
// Auth flow // Auth flow
@ -195,13 +198,13 @@ public class Program(IServiceProvider serviceProvider)
Log.Error("Auth failed"); Log.Error("Auth failed");
authService.CurrentAuth = null; authService.CurrentAuth = null;
if (!configService.CurrentConfig.DisableBrowserAuth) //if (!configService.CurrentConfig.DisableBrowserAuth)
{ //{
if (File.Exists("auth.json")) // if (File.Exists("auth.json"))
{ // {
File.Delete("auth.json"); // File.Delete("auth.json");
} // }
} //}
if (!configService.CurrentConfig.NonInteractiveMode && if (!configService.CurrentConfig.NonInteractiveMode &&
!configService.CurrentConfig.DisableBrowserAuth) !configService.CurrentConfig.DisableBrowserAuth)
@ -220,7 +223,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
} }
@ -249,7 +252,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(5); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(5);
} }
} }
@ -733,11 +736,11 @@ public class Program(IServiceProvider serviceProvider)
else if (File.Exists("auth.json")) else if (File.Exists("auth.json"))
{ {
Log.Information("Auth file found but could not be deserialized"); Log.Information("Auth file found but could not be deserialized");
if (!configService.CurrentConfig.DisableBrowserAuth) //if (!configService.CurrentConfig.DisableBrowserAuth)
{ //{
Log.Debug("Deleting auth.json"); // Log.Debug("Deleting auth.json");
File.Delete("auth.json"); // File.Delete("auth.json");
} //}
if (configService.CurrentConfig.NonInteractiveMode) if (configService.CurrentConfig.NonInteractiveMode)
{ {
@ -746,7 +749,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine( AnsiConsole.MarkupLine(
"[red]You may also want to try using the browser extension which is documented here:[/]\n"); "[red]You may also want to try using the browser extension which is documented here:[/]\n");
AnsiConsole.MarkupLine("[link]https://docs.ofdl.tools/config/auth/#legacy-methods[/]"); AnsiConsole.MarkupLine("[link]https://docs.ofdl.tools/config/auth/#legacy-methods[/]");
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
if (!configService.CurrentConfig.DisableBrowserAuth) if (!configService.CurrentConfig.DisableBrowserAuth)
@ -775,7 +778,7 @@ public class Program(IServiceProvider serviceProvider)
} }
} }
private static void ShowAuthMissingError(bool nonInteractiveMode) private void ShowAuthMissingError(bool nonInteractiveMode)
{ {
AnsiConsole.MarkupLine( AnsiConsole.MarkupLine(
"\n[red]auth.json is missing. The file can be generated automatically when OF-DL is run in the standard, interactive mode.[/]\n"); "\n[red]auth.json is missing. The file can be generated automatically when OF-DL is run in the standard, interactive mode.[/]\n");
@ -789,7 +792,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
private static void DisplayVersionResult(VersionCheckResult result) private static void DisplayVersionResult(VersionCheckResult result)
@ -879,7 +882,7 @@ public class Program(IServiceProvider serviceProvider)
} }
} }
private static void DisplayRulesJsonResult(StartupResult result, IConfigService configService) private void DisplayRulesJsonResult(StartupResult result, IConfigService configService)
{ {
if (result.RulesJsonExists) if (result.RulesJsonExists)
{ {
@ -899,7 +902,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
} }
} }