From 3d3876c9a94b86e25b6d663d65285e220fc93bcc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 13 Oct 2013 17:22:25 -0400 Subject: [PATCH] better crash logging --- .../HttpClientManager/HttpClientManager.cs | 31 +++++++++++++++++-- MediaBrowser.Mono.userprefs | 16 +++++----- ...MediaBrowser.Server.Implementations.csproj | 1 - .../MediaBrowser.Server.Mono.csproj | 3 ++ MediaBrowser.Server.Mono/Program.cs | 15 ++++++++- MediaBrowser.ServerApplication/MainStartup.cs | 13 +++++++- 6 files changed, 66 insertions(+), 13 deletions(-) diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 4d1a9c0d4a..b75234107f 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -471,7 +471,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager foreach (var pair in options.RequestHeaders.ToList()) { - message.Headers.Add(pair.Key, pair.Value); + if (!message.Headers.TryAddWithoutValidation(pair.Key, pair.Value)) + { + _logger.Error("Unable to add request header {0} with value {1}", pair.Key, pair.Value); + } } return message; @@ -484,9 +487,31 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// System.Nullable{System.Int64}. private long? GetContentLength(HttpResponseMessage response) { - IEnumerable lengthValues; + IEnumerable lengthValues = null; - if (!response.Headers.TryGetValues("content-length", out lengthValues) && !response.Content.Headers.TryGetValues("content-length", out lengthValues)) + // Seeing some InvalidOperationException here under mono + try + { + response.Headers.TryGetValues("content-length", out lengthValues); + } + catch (InvalidOperationException ex) + { + _logger.ErrorException("Error accessing response.Headers.TryGetValues Content-Length", ex); + } + + if (lengthValues == null) + { + try + { + response.Content.Headers.TryGetValues("content-length", out lengthValues); + } + catch (InvalidOperationException ex) + { + _logger.ErrorException("Error accessing response.Content.Headers.TryGetValues Content-Length", ex); + } + } + + if (lengthValues == null) { return null; } diff --git a/MediaBrowser.Mono.userprefs b/MediaBrowser.Mono.userprefs index c2875801e3..b773d7aef6 100644 --- a/MediaBrowser.Mono.userprefs +++ b/MediaBrowser.Mono.userprefs @@ -1,23 +1,25 @@  - + - - + + + - + + + + - - - + diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index f10eff1665..7dab3aee55 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -259,7 +259,6 @@ - diff --git a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj index de3d2d0cbd..2c99f3370a 100644 --- a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj +++ b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj @@ -135,5 +135,8 @@ PreserveNewest + + PreserveNewest + \ No newline at end of file diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.cs index 2c2390a814..ea6cca6ffb 100644 --- a/MediaBrowser.Server.Mono/Program.cs +++ b/MediaBrowser.Server.Mono/Program.cs @@ -206,7 +206,7 @@ namespace MediaBrowser.Server.Mono { var exception = (Exception)e.ExceptionObject; - _logger.ErrorException("UnhandledException", exception); + LogUnhandledException(exception); if (!Debugger.IsAttached) { @@ -214,6 +214,19 @@ namespace MediaBrowser.Server.Mono } } + private static void LogUnhandledException(Exception ex) + { + _logger.ErrorException("UnhandledException", ex); + + _appHost.LogManager.Flush (); + + var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt"); + + var builder = LogHelper.GetLogMessage(ex); + + File.WriteAllText(path, builder.ToString()); + } + /// /// Performs the update if needed. /// diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index f61b0f1d13..c6bd65750a 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -398,7 +398,7 @@ namespace MediaBrowser.ServerApplication { var exception = (Exception)e.ExceptionObject; - _logger.ErrorException("UnhandledException", exception); + LogUnhandledException(exception); _appHost.LogManager.Flush(); @@ -413,6 +413,17 @@ namespace MediaBrowser.ServerApplication } } + private static void LogUnhandledException(Exception ex) + { + _logger.ErrorException("UnhandledException", ex); + + var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt"); + + var builder = LogHelper.GetLogMessage(ex); + + File.WriteAllText(path, builder.ToString()); + } + /// /// Performs the update if needed. ///