Skip to content

Commit

Permalink
- Added global exception handling & TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
KrystianLesniak committed May 23, 2023
1 parent 6557a6b commit 763d550
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/SongsCompressor.MainManager/CompressionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public async Task Initialize(UserSettings settings)

public async Task Start()
{
//TODO: Add exception handling & logging
foreach (var engine in Engines.OrderBy(x => x.ExecutionOrder))
{
currentlyRunningEngine = engine;
Expand Down
37 changes: 1 addition & 36 deletions src/SongsCompressor.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Windows;
using System.Windows;

namespace SongsCompressor.WPF
{
Expand All @@ -11,36 +7,5 @@ namespace SongsCompressor.WPF
/// </summary>
public partial class App : Application
{
public App()
{
ConfigureLogging();
}

private void ConfigureLogging()
{
var config = new LoggingConfiguration();

var consoleTarget = new ColoredConsoleTarget
{
Name = "console",
Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}",
};
config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget, "*");

var fileTarget = new FileTarget
{
Name = "file",
FileName = "${basedir}/logs/errors.log",
};
config.AddRule(LogLevel.Error, LogLevel.Fatal, fileTarget, "*");

LogManager.Configuration = config;

AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
Logger log = LogManager.GetCurrentClassLogger();
log.Error(args.ExceptionObject as Exception);
};
}
}
}
58 changes: 57 additions & 1 deletion src/SongsCompressor.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MudBlazor.Services;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Targets;
using SongCompressor.MainManager;
using SongsCompressor.Common.Interfaces;
using SongsCompressor.Common.Interfaces.Services;
using SongsCompressor.Services.Services;
using System.Threading.Tasks;
using System;
using System.Windows;

namespace SongsCompressor.WPF
Expand All @@ -13,6 +19,7 @@ namespace SongsCompressor.WPF
/// </summary>
public partial class MainWindow : Window
{
private readonly IServiceProvider _serviceProvider;
public MainWindow()
{
InitializeComponent();
Expand All @@ -25,11 +32,60 @@ public MainWindow()

services.AddMudServices();

services.AddLogging(ConfigureLogging);

services.AddTransient<IFolderPicker, Services.FolderPicker>();
services.AddScoped<ISettingsStorage, SettingsStorage>();
services.AddSingleton<ICompressionManager, CompressionManager>();

Resources.Add("services", services.BuildServiceProvider());
_serviceProvider = services.BuildServiceProvider();
Resources.Add("services", _serviceProvider);
}

private void ConfigureLogging(ILoggingBuilder loggingBuilder)
{
var config = new LoggingConfiguration();

var consoleTarget = new ColoredConsoleTarget
{
Name = "console",
Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}",
};
config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, consoleTarget, "*");

var fileTarget = new FileTarget
{
Name = "file",
FileName = "${basedir}/logs/errors.log",
};
config.AddRule(NLog.LogLevel.Error, NLog.LogLevel.Fatal, fileTarget, "*");

loggingBuilder.AddNLog(config);

SetupExceptionHandling();
}

private void SetupExceptionHandling()
{
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
LogUnhandledException((Exception)e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");

Application.Current.DispatcherUnhandledException += (s, e) =>
{
LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException");
e.Handled = true;
};

TaskScheduler.UnobservedTaskException += (s, e) =>
{
LogUnhandledException(e.Exception, "TaskScheduler.UnobservedTaskException");
e.SetObserved();
};
}

private void LogUnhandledException(Exception exception, string source)
{
_serviceProvider.GetRequiredService<ILogger<MainWindow>>().LogError(exception, source);
}
}
}
1 change: 1 addition & 0 deletions src/SongsCompressor.WPF/Pages/Progress.razor
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
{
do
{
//TODO: Add exception handling
_progressStatus = await CompressionHandler.GetCurrentProgressStatus();
StateHasChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion src/SongsCompressor.WPF/SongsCompressor.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="7.0.86" />
<PackageReference Include="MudBlazor" Version="6.2.5" />
<PackageReference Include="NLog" Version="5.1.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
</ItemGroup>

Expand Down

0 comments on commit 763d550

Please sign in to comment.