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;
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<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;
namespace NzbDrone.Common.Contract
@ -7,5 +8,15 @@ namespace NzbDrone.Common.Contract
{
[JsonProperty("t")]
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.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<string,string> GetString();
}
}

View File

@ -54,6 +54,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="StringExtention.cs" />
<Compile Include="HttpProvider.cs" />
<Compile Include="ConfigFileProvider.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()
{
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)");

View File

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

View File

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