DB log target is removed after test/app shutdown

This commit is contained in:
Keivan Beigi 2013-07-29 17:57:38 -07:00
parent c80202bec3
commit c48ce4d9e0
2 changed files with 32 additions and 2 deletions

View File

@ -109,6 +109,13 @@ namespace NzbDrone.Core.Test.InstrumentationTests
epFile.Path.Should().BeNull();
}
[TearDown]
public void Teardown()
{
Mocker.Resolve<DatabaseTarget>().UnRegister();
}
private void VerifyLog(Log logItem, LogLevel level)
{
logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2));

View File

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