From c48ce4d9e08e583345d2f74ab8286c9ed3a544a9 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Mon, 29 Jul 2013 17:57:38 -0700 Subject: [PATCH] DB log target is removed after test/app shutdown --- .../DatabaseTargetFixture.cs | 7 +++++ .../Instrumentation/DatabaseTarget.cs | 27 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/NzbDrone.Core.Test/InstrumentationTests/DatabaseTargetFixture.cs b/NzbDrone.Core.Test/InstrumentationTests/DatabaseTargetFixture.cs index 4a7eb726b..6bb9644a9 100644 --- a/NzbDrone.Core.Test/InstrumentationTests/DatabaseTargetFixture.cs +++ b/NzbDrone.Core.Test/InstrumentationTests/DatabaseTargetFixture.cs @@ -109,6 +109,13 @@ namespace NzbDrone.Core.Test.InstrumentationTests epFile.Path.Should().BeNull(); } + + [TearDown] + public void Teardown() + { + Mocker.Resolve().UnRegister(); + } + private void VerifyLog(Log logItem, LogLevel level) { logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2)); diff --git a/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index b39a818b3..779643240 100644 --- a/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -3,11 +3,13 @@ using NLog.Config; using NLog; using NLog.Layouts; using NLog.Targets; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Instrumentation { - public class DatabaseTarget : TargetWithLayout + public class DatabaseTarget : TargetWithLayout, IHandle { private readonly ILogRepository _repository; @@ -24,10 +26,23 @@ namespace NzbDrone.Core.Instrumentation LogManager.Configuration.AddTarget("DbLogger", this); LogManager.Configuration.LoggingRules.Add(Rule); - LogManager.ConfigurationReloaded += (sender, args) => Register(); + LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded; LogManager.ReconfigExistingLoggers(); } + public void UnRegister() + { + LogManager.ConfigurationReloaded -= OnLogManagerOnConfigurationReloaded; + LogManager.Configuration.RemoveTarget("DbLogger"); + LogManager.Configuration.LoggingRules.Remove(Rule); + LogManager.ReconfigExistingLoggers(); + Dispose(); + } + + private void OnLogManagerOnConfigurationReloaded(object sender, LoggingConfigurationReloadedEventArgs args) + { + Register(); + } public LoggingRule Rule { get; set; } @@ -66,5 +81,13 @@ namespace NzbDrone.Core.Instrumentation _repository.Insert(log); } + + public void Handle(ApplicationShutdownRequested message) + { + if (LogManager.Configuration.LoggingRules.Contains(Rule)) + { + UnRegister(); + } + } } } \ No newline at end of file