Better logging for NzbDrone.Service

This commit is contained in:
kay.one 2012-02-15 22:16:57 -08:00
parent d68ae8f3f3
commit dde0432efc
8 changed files with 94 additions and 19 deletions

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace NzbDrone.Common.Contract namespace NzbDrone.Common.Contract
@ -13,6 +14,20 @@ namespace NzbDrone.Common.Contract
public string LogMessage { get; set; } public string LogMessage { get; set; }
[JsonProperty("s")] [JsonProperty("s")]
public string String { get; set; } public string String { get; set; }
protected override Dictionary<string, string> GetString()
{
var dic = new Dictionary<string, string>
{
{"ExType", Type.NullCheck()},
{"Logger", Logger.NullCheck()},
{"Message", LogMessage.NullCheck()},
{"Str", String.NullCheck()}
};
return dic;
}
} }
} }

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace NzbDrone.Common.Contract namespace NzbDrone.Common.Contract
@ -7,5 +8,15 @@ namespace NzbDrone.Common.Contract
{ {
[JsonProperty("t")] [JsonProperty("t")]
public string Title { get; set; } public string Title { get; set; }
protected override Dictionary<string, string> GetString()
{
var dic = new Dictionary<string, string>
{
{"Title", Title.NullCheck()},
};
return dic;
}
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -14,5 +15,18 @@ namespace NzbDrone.Common.Contract
[JsonProperty("u")] [JsonProperty("u")]
public Guid UGuid { get; set; } 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<string,string> GetString();
} }
} }

View File

@ -54,6 +54,7 @@
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="StringExtention.cs" />
<Compile Include="HttpProvider.cs" /> <Compile Include="HttpProvider.cs" />
<Compile Include="ConfigFileProvider.cs" /> <Compile Include="ConfigFileProvider.cs" />
<Compile Include="ConsoleProvider.cs" /> <Compile Include="ConsoleProvider.cs" />

View File

@ -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();
}
}
}

View File

@ -16,9 +16,11 @@ namespace NzbDrone.Services.Service.App_Start
public static void PreStart() public static void PreStart()
{ {
string logPath = string.Format("C:\\NLog\\{0}\\{1}\\${{shortdate}}.log", HostingEnvironment.SiteName, new EnviromentProvider().Version); 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.RegisterUdpLogger();
LogConfiguration.RegisterFileLogger(logPath, LogLevel.Trace); LogConfiguration.RegisterFileLogger(logPath, LogLevel.Trace);
LogConfiguration.RegisterFileLogger(error, LogLevel.Warn);
LogConfiguration.Reload(); LogConfiguration.Reload();
logger.Info("Logger has been configured. (App Start)"); logger.Info("Logger has been configured. (App Start)");

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Contract; using NzbDrone.Common.Contract;
using NzbDrone.Services.Service.Repository.Reporting; using NzbDrone.Services.Service.Repository.Reporting;
using PetaPoco; using PetaPoco;
@ -11,6 +13,7 @@ namespace NzbDrone.Services.Service.Controllers
public class ReportingController : Controller public class ReportingController : Controller
{ {
private readonly IDatabase _database; private readonly IDatabase _database;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private const string OK = "OK"; private const string OK = "OK";
@ -22,6 +25,8 @@ namespace NzbDrone.Services.Service.Controllers
[HttpPost] [HttpPost]
public JsonResult ParseError(ParseErrorReport parseErrorReport) public JsonResult ParseError(ParseErrorReport parseErrorReport)
{ {
logger.Trace(parseErrorReport.NullCheck());
if (ParseErrorExists(parseErrorReport.Title)) if (ParseErrorExists(parseErrorReport.Title))
return Json(OK); return Json(OK);
@ -42,6 +47,8 @@ namespace NzbDrone.Services.Service.Controllers
[HttpPost] [HttpPost]
public JsonResult ReportException(ExceptionReport exceptionReport) public JsonResult ReportException(ExceptionReport exceptionReport)
{
try
{ {
var row = new ExceptionRow(); var row = new ExceptionRow();
row.LoadBase(exceptionReport); row.LoadBase(exceptionReport);
@ -54,5 +61,12 @@ namespace NzbDrone.Services.Service.Controllers
return Json(OK); return Json(OK);
} }
catch(Exception)
{
logger.Trace(exceptionReport.NullCheck());
throw;
}
}
} }
} }

View File

@ -10,11 +10,12 @@ namespace NzbDrone.Services.Service
{ {
public class JsonModelBinder : DefaultModelBinder public class JsonModelBinder : DefaultModelBinder
{ {
private static readonly JsonSerializer serializer = new JsonSerializer();
private static readonly Logger logger = LogManager.GetCurrentClassLogger(); private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{ {
var input = "[NULL]";
try try
{ {
var request = controllerContext.HttpContext.Request; var request = controllerContext.HttpContext.Request;
@ -24,21 +25,18 @@ namespace NzbDrone.Services.Service
return base.BindModel(controllerContext, bindingContext); return base.BindModel(controllerContext, bindingContext);
} }
object deserializedObject; using (var reader = new StreamReader(request.InputStream))
using (var stream = request.InputStream)
{ {
stream.Seek(0, SeekOrigin.Begin); input = reader.ReadToEnd();
using (var reader = new StreamReader(stream))
{
deserializedObject = serializer.Deserialize(reader, bindingContext.ModelMetadata.ModelType);
}
} }
var deserializedObject = JsonConvert.DeserializeObject(input, bindingContext.ModelMetadata.ModelType);
return deserializedObject; return deserializedObject;
} }
catch (Exception e) catch (Exception e)
{ {
logger.FatalException("Error while binding model.", e); logger.FatalException("Error deserializing request. " + input, e);
throw; throw;
} }
} }