From 53c32fbe1061c01759be35a69fb5efe48f2e2a42 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Fri, 7 Jun 2013 12:00:48 -0700 Subject: [PATCH] added common global exception handler. --- .../GlobalExceptionHandlers.cs | 31 +++++++++++++++++++ NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Update/Program.cs | 6 ++-- NzbDrone/AppMain.cs | 12 +++---- 4 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs diff --git a/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs b/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs new file mode 100644 index 000000000..24fb64c61 --- /dev/null +++ b/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using NLog; + +namespace NzbDrone.Common.Instrumentation +{ + public static class GlobalExceptionHandlers + { + private static readonly Logger Logger = LogManager.GetLogger("Global"); + + public static void Register() + { + ExceptronTarget.Register(); + + AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception)); + TaskScheduler.UnobservedTaskException += ((s, e) => TaskException(e.Exception)); + } + + private static void TaskException(Exception exception) + { + Console.WriteLine("Task Error: {0}", exception); + Logger.Error("Task Error: " + exception.Message, exception); + } + + private static void AppDomainException(Exception exception) + { + Console.WriteLine("EPIC FAIL: {0}", exception); + Logger.FatalException("EPIC FAIL: " + exception.Message, exception); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index c81ca4552..62a0028a6 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -103,6 +103,7 @@ + diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index 21ed7ce2c..c9e29c9a9 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -27,10 +27,9 @@ namespace NzbDrone.Update try { Console.WriteLine("Starting NzbDrone Update Client"); - new LogglyTarget(new EnvironmentProvider()).Register(LogLevel.Debug); - - ExceptronTarget.Register(); + GlobalExceptionHandlers.Register(); + new LogglyTarget(new EnvironmentProvider()).Register(LogLevel.Debug); _container = UpdateContainerBuilder.Build(); logger.Info("Updating NzbDrone to version {0}", _container.Resolve().Version); @@ -40,7 +39,6 @@ namespace NzbDrone.Update { logger.FatalException("An error has occurred while applying update package.", e); } - } public void Start(string[] args) diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index c306a96e4..8def08d22 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -16,12 +16,13 @@ namespace NzbDrone { try { + GlobalExceptionHandlers.Register(); + new LogglyTarget(new EnvironmentProvider()).Register(LogLevel.Warn); - ExceptronTarget.Register(); + logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); - AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception)); //Check if full version .NET is installed. try @@ -61,14 +62,9 @@ namespace NzbDrone } catch (Exception e) { - AppDomainException(e); + logger.FatalException("Epic Fail " + e.Message, e); } } - public static void AppDomainException(Exception exception) - { - Console.WriteLine("EPIC FAIL: {0}", exception); - logger.FatalException("EPIC FAIL: " + exception.Message, exception); - } } } \ No newline at end of file