From dde0432efc4cfafeea1c4117f1da1b4a7d756ce2 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 15 Feb 2012 22:16:57 -0800 Subject: [PATCH] Better logging for NzbDrone.Service --- NzbDrone.Common/Contract/ExceptionReport.cs | 17 ++++++++++- NzbDrone.Common/Contract/ParseErrorReport.cs | 13 +++++++- NzbDrone.Common/Contract/ReportBase.cs | 14 +++++++++ NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Common/StringExtention.cs | 20 +++++++++++++ .../App_Start/Logging.cs | 2 ++ .../Controllers/ReportingController.cs | 30 ++++++++++++++----- .../JsonModelBinder.cs | 16 +++++----- 8 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 NzbDrone.Common/StringExtention.cs diff --git a/NzbDrone.Common/Contract/ExceptionReport.cs b/NzbDrone.Common/Contract/ExceptionReport.cs index 86ad06533..893f00af8 100644 --- a/NzbDrone.Common/Contract/ExceptionReport.cs +++ b/NzbDrone.Common/Contract/ExceptionReport.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; namespace NzbDrone.Common.Contract @@ -13,6 +14,20 @@ namespace NzbDrone.Common.Contract public string LogMessage { get; set; } [JsonProperty("s")] public string String { get; set; } + + + protected override Dictionary GetString() + { + var dic = new Dictionary + { + {"ExType", Type.NullCheck()}, + {"Logger", Logger.NullCheck()}, + {"Message", LogMessage.NullCheck()}, + {"Str", String.NullCheck()} + }; + + return dic; + } } } diff --git a/NzbDrone.Common/Contract/ParseErrorReport.cs b/NzbDrone.Common/Contract/ParseErrorReport.cs index 72b10f314..6f22e2855 100644 --- a/NzbDrone.Common/Contract/ParseErrorReport.cs +++ b/NzbDrone.Common/Contract/ParseErrorReport.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; namespace NzbDrone.Common.Contract @@ -7,5 +8,15 @@ namespace NzbDrone.Common.Contract { [JsonProperty("t")] public string Title { get; set; } + + protected override Dictionary GetString() + { + var dic = new Dictionary + { + {"Title", Title.NullCheck()}, + }; + + return dic; + } } } diff --git a/NzbDrone.Common/Contract/ReportBase.cs b/NzbDrone.Common/Contract/ReportBase.cs index 3b0c2d3a4..b581a2c53 100644 --- a/NzbDrone.Common/Contract/ReportBase.cs +++ b/NzbDrone.Common/Contract/ReportBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; @@ -14,5 +15,18 @@ namespace NzbDrone.Common.Contract [JsonProperty("u")] public Guid UGuid { get; set; } + + public override string ToString() + { + var childString = ""; + foreach (var keyValue in GetString()) + { + childString += string.Format("{0}: {1} ", keyValue.Key, keyValue.Value); + } + + return string.Format("[{0} Prd:{1} V:{2} ID:{3} | {4}]", GetType().Name, IsProduction, Version, UGuid, childString.Trim()); + } + + protected abstract Dictionary GetString(); } } diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 60cfd9aa2..b9e06a43b 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -54,6 +54,7 @@ + diff --git a/NzbDrone.Common/StringExtention.cs b/NzbDrone.Common/StringExtention.cs new file mode 100644 index 000000000..d3e35bbc5 --- /dev/null +++ b/NzbDrone.Common/StringExtention.cs @@ -0,0 +1,20 @@ +using System; +using System.Diagnostics; + +namespace NzbDrone.Common +{ + public static class StringExtention + { + + public static object NullCheck(this object target) + { + if (target != null) return target; + return "[NULL]"; + } + + public static string NullCheck(this string target) + { + return ((object)target).NullCheck().ToString(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs b/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs index cdae09e7b..7c7364eea 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs +++ b/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs @@ -16,9 +16,11 @@ namespace NzbDrone.Services.Service.App_Start public static void PreStart() { string logPath = string.Format("C:\\NLog\\{0}\\{1}\\${{shortdate}}.log", HostingEnvironment.SiteName, new EnviromentProvider().Version); + string error = string.Format("C:\\NLog\\{0}\\{1}\\${{shortdate}}.Error.log", HostingEnvironment.SiteName, new EnviromentProvider().Version); LogConfiguration.RegisterUdpLogger(); LogConfiguration.RegisterFileLogger(logPath, LogLevel.Trace); + LogConfiguration.RegisterFileLogger(error, LogLevel.Warn); LogConfiguration.Reload(); logger.Info("Logger has been configured. (App Start)"); diff --git a/NzbDrone.Services/NzbDrone.Services.Service/Controllers/ReportingController.cs b/NzbDrone.Services/NzbDrone.Services.Service/Controllers/ReportingController.cs index 1042501ab..793ddb158 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/Controllers/ReportingController.cs +++ b/NzbDrone.Services/NzbDrone.Services.Service/Controllers/ReportingController.cs @@ -1,6 +1,8 @@ using System; using System.Linq; using System.Web.Mvc; +using NLog; +using NzbDrone.Common; using NzbDrone.Common.Contract; using NzbDrone.Services.Service.Repository.Reporting; using PetaPoco; @@ -11,6 +13,7 @@ namespace NzbDrone.Services.Service.Controllers public class ReportingController : Controller { private readonly IDatabase _database; + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); private const string OK = "OK"; @@ -22,6 +25,8 @@ namespace NzbDrone.Services.Service.Controllers [HttpPost] public JsonResult ParseError(ParseErrorReport parseErrorReport) { + logger.Trace(parseErrorReport.NullCheck()); + if (ParseErrorExists(parseErrorReport.Title)) return Json(OK); @@ -43,16 +48,25 @@ namespace NzbDrone.Services.Service.Controllers [HttpPost] public JsonResult ReportException(ExceptionReport exceptionReport) { - var row = new ExceptionRow(); - row.LoadBase(exceptionReport); - row.LogMessage = exceptionReport.LogMessage; - row.Logger = exceptionReport.Logger; - row.String = exceptionReport.String; - row.Type = exceptionReport.Type; + try + { + var row = new ExceptionRow(); + row.LoadBase(exceptionReport); + row.LogMessage = exceptionReport.LogMessage; + row.Logger = exceptionReport.Logger; + row.String = exceptionReport.String; + row.Type = exceptionReport.Type; - _database.Insert(row); + _database.Insert(row); - return Json(OK); + return Json(OK); + } + catch(Exception) + { + logger.Trace(exceptionReport.NullCheck()); + throw; + } + } } } \ No newline at end of file diff --git a/NzbDrone.Services/NzbDrone.Services.Service/JsonModelBinder.cs b/NzbDrone.Services/NzbDrone.Services.Service/JsonModelBinder.cs index 2f59faee6..94c7a404d 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/JsonModelBinder.cs +++ b/NzbDrone.Services/NzbDrone.Services.Service/JsonModelBinder.cs @@ -10,11 +10,12 @@ namespace NzbDrone.Services.Service { public class JsonModelBinder : DefaultModelBinder { - private static readonly JsonSerializer serializer = new JsonSerializer(); private static readonly Logger logger = LogManager.GetCurrentClassLogger(); public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { + var input = "[NULL]"; + try { var request = controllerContext.HttpContext.Request; @@ -24,21 +25,18 @@ namespace NzbDrone.Services.Service return base.BindModel(controllerContext, bindingContext); } - object deserializedObject; - using (var stream = request.InputStream) + using (var reader = new StreamReader(request.InputStream)) { - stream.Seek(0, SeekOrigin.Begin); - using (var reader = new StreamReader(stream)) - { - deserializedObject = serializer.Deserialize(reader, bindingContext.ModelMetadata.ModelType); - } + input = reader.ReadToEnd(); } + var deserializedObject = JsonConvert.DeserializeObject(input, bindingContext.ModelMetadata.ModelType); + return deserializedObject; } catch (Exception e) { - logger.FatalException("Error while binding model.", e); + logger.FatalException("Error deserializing request. " + input, e); throw; } }