From 1d1c62d376af3911f177b97b547de8c3ef176adc Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 5 Feb 2012 08:53:08 -0800 Subject: [PATCH] added file logging to services. --- ...portingService_ReportParseError_Fixture.cs | 11 ++++- ...{ParseErrorRequ.cs => ParseErrorReport.cs} | 0 NzbDrone.Common/LogConfiguration.cs | 2 +- NzbDrone.Common/NzbDrone.Common.csproj | 2 +- NzbDrone.Common/ReportingService.cs | 6 +-- NzbDrone.Common/RestProvider.cs | 49 ++++++++++++------- .../App_Start/Logging.cs | 26 ++++++++++ .../Migrations/Migration20120205.cs | 22 +++++++++ .../Migrations/MigrationHelper.cs | 2 +- .../NzbDrone.Services.Service.csproj | 2 + .../Web.Release.config | 2 +- .../ReportingControllerFixture.cs | 4 +- 12 files changed, 100 insertions(+), 28 deletions(-) rename NzbDrone.Common/Contract/{ParseErrorRequ.cs => ParseErrorReport.cs} (100%) create mode 100644 NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs create mode 100644 NzbDrone.Services/NzbDrone.Services.Service/Migrations/Migration20120205.cs diff --git a/NzbDrone.Common.Test/ReportingService_ReportParseError_Fixture.cs b/NzbDrone.Common.Test/ReportingService_ReportParseError_Fixture.cs index abc1c291d..17fccf7b1 100644 --- a/NzbDrone.Common.Test/ReportingService_ReportParseError_Fixture.cs +++ b/NzbDrone.Common.Test/ReportingService_ReportParseError_Fixture.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Common.Test const string badTitle = "Bad Title"; ReportingService.ReportParseError(badTitle); - MockedRestProvider.Verify(p => p.PostData(It.Is(c => c.ToLower().StartsWith("http://service.nzbdrone.com/")), It.Is(c => c.Title == badTitle)), Times.Once()); + MockedRestProvider.Verify(p => p.PostData(It.Is(c => c.ToLower().StartsWith("http://services.nzbdrone.com/")), It.Is(c => c.Title == badTitle)), Times.Once()); } [Test] @@ -64,5 +64,14 @@ namespace NzbDrone.Common.Test MockedRestProvider.Verify(p => p.PostData(It.IsAny(), It.Is(c => c.Title == "title 2")), Times.Once()); } + + [Test] + public void report_parse_error() + { + ReportingService.RestProvider = new RestProvider(new EnviromentProvider()); + ReportingService.ReportParseError("Test error"); + + } + } } \ No newline at end of file diff --git a/NzbDrone.Common/Contract/ParseErrorRequ.cs b/NzbDrone.Common/Contract/ParseErrorReport.cs similarity index 100% rename from NzbDrone.Common/Contract/ParseErrorRequ.cs rename to NzbDrone.Common/Contract/ParseErrorReport.cs diff --git a/NzbDrone.Common/LogConfiguration.cs b/NzbDrone.Common/LogConfiguration.cs index cf9469147..6722caade 100644 --- a/NzbDrone.Common/LogConfiguration.cs +++ b/NzbDrone.Common/LogConfiguration.cs @@ -86,7 +86,7 @@ namespace NzbDrone.Common fileTarget.ConcurrentWriteAttemptDelay = 50; fileTarget.ConcurrentWriteAttempts = 200; - fileTarget.Layout = @"${date:format=yy-M-d HH\:mm\:ss.f}|${replace:searchFor=NzbDrone.:replaceWith=:inner=${logger}}|${message}|${exception:format=ToString}"; + fileTarget.Layout = @"${date:format=yy-M-d HH\:mm\:ss.f}|${replace:searchFor=NzbDrone.:replaceWith=:inner=${logger}}|${level}|${message}|${exception:format=ToString}"; return fileTarget; } diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index ec6bc1cf9..d77c252e2 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -57,7 +57,7 @@ - + diff --git a/NzbDrone.Common/ReportingService.cs b/NzbDrone.Common/ReportingService.cs index 83b727cfc..dd0d8d2cb 100644 --- a/NzbDrone.Common/ReportingService.cs +++ b/NzbDrone.Common/ReportingService.cs @@ -10,9 +10,9 @@ namespace NzbDrone.Common { private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - private const string SERVICE_URL = "http://service.nzbdrone.com/reporting"; - private const string PARSE_URL = SERVICE_URL + "/parser"; - private const string EXCEPTION_URL = SERVICE_URL + "/exception"; + private const string SERVICE_URL = "http://services.nzbdrone.com/reporting"; + private const string PARSE_URL = SERVICE_URL + "/ParseError"; + private const string EXCEPTION_URL = SERVICE_URL + "/ReportException"; public static RestProvider RestProvider { get; set; } private static readonly HashSet parserErrorCache = new HashSet(); diff --git a/NzbDrone.Common/RestProvider.cs b/NzbDrone.Common/RestProvider.cs index b8b8a93c3..4dc4da867 100644 --- a/NzbDrone.Common/RestProvider.cs +++ b/NzbDrone.Common/RestProvider.cs @@ -1,7 +1,9 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Net; using System.Text; +using NLog; using Newtonsoft.Json; using Ninject; using NzbDrone.Common.Contract; @@ -11,6 +13,9 @@ namespace NzbDrone.Common public class RestProvider { + + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + private readonly EnviromentProvider _enviromentProvider; @@ -25,7 +30,7 @@ namespace NzbDrone.Common } - private const int TIMEOUT = 10000; + private const int TIMEOUT = 15000; private const string METHOD = "POST"; public virtual void PostData(string url, ReportBase reportBase) @@ -40,28 +45,36 @@ namespace NzbDrone.Common private static void PostData(string url, object message) { - var json = JsonConvert.SerializeObject(message); + try + { + var json = JsonConvert.SerializeObject(message); - var request = (HttpWebRequest)WebRequest.Create(url); - request.Timeout = TIMEOUT; + var request = (HttpWebRequest)WebRequest.Create(url); + request.Timeout = TIMEOUT; - request.Proxy = WebRequest.DefaultWebProxy; + request.Proxy = WebRequest.DefaultWebProxy; - request.KeepAlive = false; - request.ProtocolVersion = HttpVersion.Version10; - request.Method = METHOD; - request.ContentType = "application/json"; + request.KeepAlive = false; + request.ProtocolVersion = HttpVersion.Version10; + request.Method = METHOD; + request.ContentType = "application/json"; - byte[] postBytes = Encoding.UTF8.GetBytes(json); - request.ContentLength = postBytes.Length; + byte[] postBytes = Encoding.UTF8.GetBytes(json); + request.ContentLength = postBytes.Length; - var requestStream = request.GetRequestStream(); - requestStream.Write(postBytes, 0, postBytes.Length); - requestStream.Close(); + var requestStream = request.GetRequestStream(); + requestStream.Write(postBytes, 0, postBytes.Length); + requestStream.Close(); - var response = (HttpWebResponse)request.GetResponse(); - var streamreader = new StreamReader(response.GetResponseStream()); - streamreader.Close(); + var response = (HttpWebResponse)request.GetResponse(); + var streamreader = new StreamReader(response.GetResponseStream()); + streamreader.Close(); + } + catch (Exception e) + { + e.Data.Add("URL", url); + throw; + } } } } diff --git a/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs b/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs new file mode 100644 index 000000000..3d453defc --- /dev/null +++ b/NzbDrone.Services/NzbDrone.Services.Service/App_Start/Logging.cs @@ -0,0 +1,26 @@ +using System.Linq; +using NLog; +using NzbDrone.Common; +using NzbDrone.Services.Service.App_Start; + +[assembly: WebActivator.PreApplicationStartMethod(typeof(Logging), "PreStart")] + +namespace NzbDrone.Services.Service.App_Start +{ + + public static class Logging + { + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + + public static void PreStart() + { + LogConfiguration.RegisterUdpLogger(); + LogConfiguration.RegisterFileLogger("${basedir}/_logs/${shortdate}.log", LogLevel.Trace); + LogConfiguration.Reload(); + + logger.Info("Logger has been configured. (App Start)"); + + + } + } +} \ No newline at end of file diff --git a/NzbDrone.Services/NzbDrone.Services.Service/Migrations/Migration20120205.cs b/NzbDrone.Services/NzbDrone.Services.Service/Migrations/Migration20120205.cs new file mode 100644 index 000000000..90b1f39c8 --- /dev/null +++ b/NzbDrone.Services/NzbDrone.Services.Service/Migrations/Migration20120205.cs @@ -0,0 +1,22 @@ +using System; +using System.Data; +using Migrator.Framework; + +namespace NzbDrone.Services.Service.Migrations +{ + + [Migration(20120205)] + public class Migration20120205 : Migration + { + public override void Up() + { + Database.ChangeColumn("ParseErrorReports", MigrationsHelper.VersionColumn); + Database.ChangeColumn("ExceptionReports", MigrationsHelper.VersionColumn); + } + + public override void Down() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Services/NzbDrone.Services.Service/Migrations/MigrationHelper.cs b/NzbDrone.Services/NzbDrone.Services.Service/Migrations/MigrationHelper.cs index 162847025..689eb9b08 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/Migrations/MigrationHelper.cs +++ b/NzbDrone.Services/NzbDrone.Services.Service/Migrations/MigrationHelper.cs @@ -46,7 +46,7 @@ namespace NzbDrone.Services.Service.Migrations return String.Format("IX_{0}_{1}", tableName, String.Join("_", columns)); } - public static readonly Column VersionColumn = new Column("Version", DbType.String, 10, ColumnProperty.NotNull); + public static readonly Column VersionColumn = new Column("Version", DbType.String, 50, ColumnProperty.NotNull); public static readonly Column ProductionColumn = new Column("IsProduction", DbType.Boolean, ColumnProperty.NotNull); public static readonly Column TimestampColumn = new Column("TimeStamp", DbType.DateTime, ColumnProperty.NotNull); public static readonly Column UGuidColumn = new Column("UGuid", DbType.Guid, ColumnProperty.Null); diff --git a/NzbDrone.Services/NzbDrone.Services.Service/NzbDrone.Services.Service.csproj b/NzbDrone.Services/NzbDrone.Services.Service/NzbDrone.Services.Service.csproj index 156c81011..b99c9e284 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/NzbDrone.Services.Service.csproj +++ b/NzbDrone.Services/NzbDrone.Services.Service/NzbDrone.Services.Service.csproj @@ -108,6 +108,7 @@ + @@ -119,6 +120,7 @@ + diff --git a/NzbDrone.Services/NzbDrone.Services.Service/Web.Release.config b/NzbDrone.Services/NzbDrone.Services.Service/Web.Release.config index 06e1f61a3..644c302b9 100644 --- a/NzbDrone.Services/NzbDrone.Services.Service/Web.Release.config +++ b/NzbDrone.Services/NzbDrone.Services.Service/Web.Release.config @@ -16,7 +16,7 @@ --> - +