Skip to content

Commit

Permalink
Implemented rudimentary crash log dumping.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdoombox committed Mar 11, 2020
1 parent 517aefe commit e57ecef
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 6 deletions.
52 changes: 52 additions & 0 deletions PostScriptumMortarCalculator/Bootstrapper/ErrorLogger.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
25 changes: 25 additions & 0 deletions PostScriptumMortarCalculator/Bootstrapper/PSMCBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PsmcRootViewModel>
{
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<ConfigService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c">
<HintPath>..\packages\NLog.4.6.8\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Octokit, Version=0.43.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\Octokit.0.43.0\lib\net46\Octokit.dll</HintPath>
<Private>True</Private>
Expand All @@ -89,9 +93,13 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
Expand All @@ -113,6 +121,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Bootstrapper\ErrorLogger.cs" />
<Compile Include="Converters\BooleanConverterBase.cs" />
<Compile Include="Converters\BooleanToVisibilityConverter.cs" />
<Compile Include="Converters\HalfDoubleConverter.cs" />
Expand Down
6 changes: 3 additions & 3 deletions PostScriptumMortarCalculator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("1.3")]
[assembly: AssemblyFileVersion("1.3")]
[assembly: AssemblyInformationalVersion("1.3")]
2 changes: 2 additions & 0 deletions PostScriptumMortarCalculator/Services/UpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if (!DEBUG)
using System.Reflection;
#endif
using System.Threading.Tasks;
using Octokit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.Controls;
using PostScriptumMortarCalculator.Services;
using Stylet;
Expand Down
2 changes: 0 additions & 2 deletions PostScriptumMortarCalculator/Views/CreditsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}">
Expand Down
1 change: 1 addition & 0 deletions PostScriptumMortarCalculator/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<package id="MahApps.Metro" version="2.0.0-alpha0660" targetFramework="net48" />
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.3" targetFramework="net48" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net48" />
<package id="NLog" version="4.6.8" targetFramework="net48" />
<package id="Octokit" version="0.43.0" targetFramework="net48" />
<package id="PanAndZoom" version="2.3.1" targetFramework="net48" />
<package id="PropertyChanged.Fody" version="3.2.6" targetFramework="net48" />
Expand Down

0 comments on commit e57ecef

Please sign in to comment.