diff --git a/PostScriptumMortarCalculator/Bootstrapper/ErrorLogger.cs b/PostScriptumMortarCalculator/Bootstrapper/ErrorLogger.cs new file mode 100644 index 0000000..86b3d4b --- /dev/null +++ b/PostScriptumMortarCalculator/Bootstrapper/ErrorLogger.cs @@ -0,0 +1,52 @@ +using System; +using System.Reflection; +using System.Threading.Tasks; +using NLog; +using System.Windows; + +namespace PostScriptumMortarCalculator.Bootstrapper +{ + public class ErrorLogger + { + private static readonly Logger m_logger = LogManager.GetCurrentClassLogger(); + public ErrorLogger() + { + 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) + { + var message = $"Unhandled exception ({source})"; + try + { + var assemblyName = Assembly.GetExecutingAssembly().GetName(); + message = $"Unhandled exception in {assemblyName.Name} v{assemblyName.Version}"; + } + catch (Exception ex) + { + m_logger.Trace(ex, "Exception in LogUnhandledException"); + LogManager.Shutdown(); + Application.Current.Shutdown(); + } + finally + { + m_logger.Trace(exception, message); + LogManager.Shutdown(); + Application.Current.Shutdown(); + } + } + } +} \ No newline at end of file diff --git a/PostScriptumMortarCalculator/Bootstrapper/PSMCBootstrapper.cs b/PostScriptumMortarCalculator/Bootstrapper/PSMCBootstrapper.cs index 6e6725c..fa2fe93 100644 --- a/PostScriptumMortarCalculator/Bootstrapper/PSMCBootstrapper.cs +++ b/PostScriptumMortarCalculator/Bootstrapper/PSMCBootstrapper.cs @@ -5,11 +5,36 @@ using PostScriptumMortarCalculator.ViewModels; using Stylet; using StyletIoC; +#if (!DEBUG) +using LogLevel = NLog.LogLevel; +using NLog.Config; +using NLog.Targets; +#endif namespace PostScriptumMortarCalculator.Bootstrapper { public class PsmcBootstrapper : Bootstrapper { + private const string c_LOG_DUMP_FILE_NAME = "ErrorLog.log"; + + private ErrorLogger m_logger; + protected override void OnStart() + { +#if (!DEBUG) + var config = new LoggingConfiguration(); + var logFile = new FileTarget("logfile") + { + CreateDirs = true, + FileName = c_LOG_DUMP_FILE_NAME, + DeleteOldFileOnStartup = true, + Layout = "${longdate} ${message} ${exception:format=tostring}" + }; + config.AddRule(LogLevel.Trace, LogLevel.Trace, logFile); + NLog.LogManager.Configuration = config; + m_logger = new ErrorLogger(); +#endif + } + protected override void ConfigureIoC(IStyletIoCBuilder builder) { builder.Bind() diff --git a/PostScriptumMortarCalculator/PostScriptumMortarCalculator.csproj b/PostScriptumMortarCalculator/PostScriptumMortarCalculator.csproj index 0b46843..09eec65 100644 --- a/PostScriptumMortarCalculator/PostScriptumMortarCalculator.csproj +++ b/PostScriptumMortarCalculator/PostScriptumMortarCalculator.csproj @@ -68,6 +68,10 @@ ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll True + + ..\packages\NLog.4.6.8\lib\net45\NLog.dll + True + ..\packages\Octokit.0.43.0\lib\net46\Octokit.dll True @@ -89,9 +93,13 @@ + + + + 4.0 @@ -113,6 +121,7 @@ MSBuild:Compile Designer + diff --git a/PostScriptumMortarCalculator/Properties/AssemblyInfo.cs b/PostScriptumMortarCalculator/Properties/AssemblyInfo.cs index 4a58f9a..58ba7b8 100644 --- a/PostScriptumMortarCalculator/Properties/AssemblyInfo.cs +++ b/PostScriptumMortarCalculator/Properties/AssemblyInfo.cs @@ -49,6 +49,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1")] -[assembly: AssemblyFileVersion("1.1")] -[assembly: AssemblyInformationalVersion("1.1")] \ No newline at end of file +[assembly: AssemblyVersion("1.3")] +[assembly: AssemblyFileVersion("1.3")] +[assembly: AssemblyInformationalVersion("1.3")] \ No newline at end of file diff --git a/PostScriptumMortarCalculator/Services/UpdateService.cs b/PostScriptumMortarCalculator/Services/UpdateService.cs index 4123f33..0d8a883 100644 --- a/PostScriptumMortarCalculator/Services/UpdateService.cs +++ b/PostScriptumMortarCalculator/Services/UpdateService.cs @@ -1,4 +1,6 @@ +#if (!DEBUG) using System.Reflection; +#endif using System.Threading.Tasks; using Octokit; diff --git a/PostScriptumMortarCalculator/ViewModels/PsmcRootViewModel.cs b/PostScriptumMortarCalculator/ViewModels/PsmcRootViewModel.cs index 7902a4f..01de769 100644 --- a/PostScriptumMortarCalculator/ViewModels/PsmcRootViewModel.cs +++ b/PostScriptumMortarCalculator/ViewModels/PsmcRootViewModel.cs @@ -1,5 +1,4 @@ using System.Windows; -using System.Windows.Controls; using MahApps.Metro.Controls; using PostScriptumMortarCalculator.Services; using Stylet; diff --git a/PostScriptumMortarCalculator/Views/CreditsView.xaml b/PostScriptumMortarCalculator/Views/CreditsView.xaml index 0f3fd5c..4326f41 100644 --- a/PostScriptumMortarCalculator/Views/CreditsView.xaml +++ b/PostScriptumMortarCalculator/Views/CreditsView.xaml @@ -3,9 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - xmlns:local="clr-namespace:PostScriptumMortarCalculator.Views" xmlns:viewModels="clr-namespace:PostScriptumMortarCalculator.ViewModels" - xmlns:s="https://github.com/canton7/Stylet" x:Name="RootControl" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:CreditsViewModel}"> diff --git a/PostScriptumMortarCalculator/packages.config b/PostScriptumMortarCalculator/packages.config index 5682374..3f7e7b2 100644 --- a/PostScriptumMortarCalculator/packages.config +++ b/PostScriptumMortarCalculator/packages.config @@ -7,6 +7,7 @@ +