diff --git a/src/NzbDrone.Api/Extensions/Pipelines/RequestLoggingPipeline.cs b/src/NzbDrone.Api/Extensions/Pipelines/RequestLoggingPipeline.cs index fbff775ba..470e7d18c 100644 --- a/src/NzbDrone.Api/Extensions/Pipelines/RequestLoggingPipeline.cs +++ b/src/NzbDrone.Api/Extensions/Pipelines/RequestLoggingPipeline.cs @@ -1,8 +1,10 @@ using System; +using System.Linq; using System.Threading; using Nancy; using Nancy.Bootstrapper; using NLog; +using NzbDrone.Api.ErrorManagement; using NzbDrone.Common.Extensions; namespace NzbDrone.Api.Extensions.Pipelines @@ -14,12 +16,20 @@ namespace NzbDrone.Api.Extensions.Pipelines private static int _requestSequenceID; + private readonly NzbDroneErrorPipeline _errorPipeline; + + public RequestLoggingPipeline(NzbDroneErrorPipeline errorPipeline) + { + _errorPipeline = errorPipeline; + } + public int Order { get { return 100; } } public void Register(IPipelines pipelines) { pipelines.BeforeRequest.AddItemToStartOfPipeline(LogStart); pipelines.AfterRequest.AddItemToEndOfPipeline(LogEnd); + pipelines.OnError.AddItemToEndOfPipeline(LogError); } private Response LogStart(NancyContext context) @@ -54,11 +64,24 @@ namespace NzbDrone.Api.Extensions.Pipelines } } + private Response LogError(NancyContext context, Exception exception) + { + var response = _errorPipeline.HandleException(context, exception); + + context.Response = response; + + LogEnd(context); + + context.Response = null; + + return response; + } + private static string GetRequestPathAndQuery(Request request) { if (request.Url.Query.IsNotNullOrWhiteSpace()) { - return string.Concat(request.Url.Path, "?", request.Url.Query); + return string.Concat(request.Url.Path, request.Url.Query); } else { diff --git a/src/NzbDrone.Api/NancyBootstrapper.cs b/src/NzbDrone.Api/NancyBootstrapper.cs index 2b8ca3369..0fe365808 100644 --- a/src/NzbDrone.Api/NancyBootstrapper.cs +++ b/src/NzbDrone.Api/NancyBootstrapper.cs @@ -38,8 +38,6 @@ namespace NzbDrone.Api container.Resolve().Register(); container.Resolve().PublishEvent(new ApplicationStartedEvent()); - - ApplicationPipelines.OnError.AddItemToEndOfPipeline((Func) container.Resolve().HandleException); } private void RegisterPipelines(IPipelines pipelines) diff --git a/src/NzbDrone.Integration.Test/HttpLogFixture.cs b/src/NzbDrone.Integration.Test/HttpLogFixture.cs new file mode 100644 index 000000000..dfa2d722f --- /dev/null +++ b/src/NzbDrone.Integration.Test/HttpLogFixture.cs @@ -0,0 +1,30 @@ +using System.IO; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; + +namespace NzbDrone.Integration.Test +{ + [TestFixture] + public class HttpLogFixture : IntegrationTest + { + [Test] + public void should_log_on_error() + { + var config = HostConfig.Get(1); + config.LogLevel = "Trace"; + HostConfig.Put(config); + + var logFile = Path.Combine(_runner.AppData, "logs", "sonarr.trace.txt"); + var logLines = File.ReadAllLines(logFile); + + var result = Series.InvalidPost(new Api.Series.SeriesResource()); + + logLines = File.ReadAllLines(logFile).Skip(logLines.Length).ToArray(); + + logLines.Should().Contain(v => v.Contains("|Trace|Http|Req")); + logLines.Should().Contain(v => v.Contains("|Trace|Http|Res")); + logLines.Should().Contain(v => v.Contains("|Debug|Api|")); + } + } +} diff --git a/src/NzbDrone.Integration.Test/IntegrationTest.cs b/src/NzbDrone.Integration.Test/IntegrationTest.cs index a4e4c58c8..64efaa527 100644 --- a/src/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/src/NzbDrone.Integration.Test/IntegrationTest.cs @@ -9,7 +9,7 @@ namespace NzbDrone.Integration.Test { public abstract class IntegrationTest : IntegrationTestBase { - private NzbDroneRunner _runner; + protected NzbDroneRunner _runner; public override string SeriesRootFolder { diff --git a/src/NzbDrone.Integration.Test/IntegrationTestBase.cs b/src/NzbDrone.Integration.Test/IntegrationTestBase.cs index ad34134ab..e587a9aef 100644 --- a/src/NzbDrone.Integration.Test/IntegrationTestBase.cs +++ b/src/NzbDrone.Integration.Test/IntegrationTestBase.cs @@ -44,6 +44,7 @@ namespace NzbDrone.Integration.Test public DownloadClientClient DownloadClients; public EpisodeClient Episodes; public ClientBase History; + public ClientBase HostConfig; public IndexerClient Indexers; public ClientBase NamingConfig; public NotificationClient Notifications; @@ -109,6 +110,7 @@ namespace NzbDrone.Integration.Test DownloadClients = new DownloadClientClient(RestClient, ApiKey); Episodes = new EpisodeClient(RestClient, ApiKey); History = new ClientBase(RestClient, ApiKey); + HostConfig = new ClientBase(RestClient, ApiKey, "config/host"); Indexers = new IndexerClient(RestClient, ApiKey); NamingConfig = new ClientBase(RestClient, ApiKey, "config/naming"); Notifications = new NotificationClient(RestClient, ApiKey); diff --git a/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj b/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj index c03fc46f5..b407b82ca 100644 --- a/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj +++ b/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj @@ -124,6 +124,7 @@ +