Skip to content

Instantly share code, notes, and snippets.

@tomspilman
Created March 27, 2018 23:51
Show Gist options
  • Save tomspilman/470c6466508ddd52a338b2be036b088e to your computer and use it in GitHub Desktop.
Save tomspilman/470c6466508ddd52a338b2be036b088e to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Sickhead.Engine.Debugging;
using Sickhead.Engine.Util;
using System.Diagnostics;
namespace Sawmill.Common
{
static public class CrashLogger
{
private static string _gameName;
private static DateTime _startTime;
static public void Initialize(string name)
{
_gameName = name;
_startTime = DateTime.UtcNow;
#if !SHIPPING
if (!Debugger.IsAttached)
#endif
AppDomain.CurrentDomain.UnhandledException += OnException;
}
private static void OnException(object sender, UnhandledExceptionEventArgs args)
{
OnException(args.ExceptionObject);
}
private static void OnException(object exceptionObject)
{
try
{
try
{
// Gather the data to be logged.
var logging = _gameName + " - " +
typeof(CrashLogger).Assembly.GetVersionString(true) + " - " +
Environment.MachineName + " - " +
(DateTime.UtcNow - _startTime) +
Environment.NewLine +
Environment.NewLine;
logging += exceptionObject.ToString();
// Write out the log.
var fileName = string.Format("crashlog-{0:MM-dd-yyyy_hh-mm-ss-tt}.txt", DateTime.Now);
File.WriteAllText(fileName, logging);
// Open the crash log file for the user.
#if !SHIPPING
Process.Start(fileName);
#endif
}
catch
{
var fileName = string.Format("crashlog-{0:MM-dd-yyyy_hh-mm-ss-tt}.txt", DateTime.Now);
File.WriteAllText(fileName, "UnhandledException handler itself threw an exception...");
}
}
finally
{
// Force shutdown on exception!
Environment.Exit(-1);
}
}
[ConsoleMethod]
static public void CrashTest()
{
try
{
throw new Exception("This is a crash!");
}
catch (Exception ex)
{
OnException(ex);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment