Skip to content

Commit e9ad54d

Browse files
committed
Add Serilog logging and refactor configuration handling
- Implemented Serilog in Program.cs for enhanced logging during application startup, including error handling. - Updated Retroworks.RCBus.csproj to include Serilog and Serilog.Sinks.File package references. - Modified Settings.cs to use a relative path for the configuration file, simplifying file handling.
1 parent 9e96c06 commit e9ad54d

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

Retroworks.RCBus/Program.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using Avalonia;
3+
using Serilog;
4+
using System.IO;
35

46
namespace Retroworks.RCBus
57
{
@@ -9,10 +11,39 @@ internal sealed class Program
911
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
1012
// yet and stuff might break.
1113
[STAThread]
12-
public static void Main(string[] args) => BuildAvaloniaApp()
13-
.StartWithClassicDesktopLifetime(args);
14+
public static void Main(string[] args)
15+
{
16+
// Set current directory to a writable location
17+
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
18+
var cwd = Path.Combine(appData, "Retroworks.RCBus");
19+
Directory.CreateDirectory(cwd);
20+
Directory.SetCurrentDirectory(cwd);
21+
string logPath = Path.Combine(cwd, "Retroworks.RCBus-.log");
22+
23+
// Configure Serilog
24+
Log.Logger = new LoggerConfiguration()
25+
.MinimumLevel.Debug()
26+
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
27+
.CreateLogger();
28+
Log.Information("Program starting...");
29+
Log.Information($"Working directory: {cwd}");
30+
31+
try
32+
{
33+
BuildAvaloniaApp()
34+
.StartWithClassicDesktopLifetime(args);
35+
}
36+
catch (Exception ex)
37+
{
38+
Log.Fatal(ex, "Application start-up failed");
39+
throw;
40+
}
41+
finally
42+
{
43+
Log.CloseAndFlush();
44+
}
45+
}
1446

15-
// Avalonia configuration, don't remove; also used by visual designer.
1647
public static AppBuilder BuildAvaloniaApp()
1748
=> AppBuilder.Configure<App>()
1849
.UsePlatformDetect()

Retroworks.RCBus/Retroworks.RCBus.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
3737
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
3838
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
39+
<PackageReference Include="Serilog" Version="4.3.0" />
40+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
3941
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />
4042
<PackageReference Include="SkiaSharp.NativeAssets.WebAssembly" Version="3.116.1" />
4143
<PackageReference Include="System.Configuration.ConfigurationManager" Version="9.0.4" />

Retroworks.RCBus/Services/Internals/Settings.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ internal class Settings : ISettings
1111

1212
public Settings()
1313
{
14-
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
15-
string configDir = Path.Combine(appData, "Retroworks.RCBus");
16-
Directory.CreateDirectory(configDir);
17-
18-
string configPath = Path.Combine(configDir, "user.config");
14+
string configPath = "user.config";
1915

2016
// If config file doesn't exist, copy from default
2117
if (!File.Exists(configPath))

0 commit comments

Comments
 (0)