Lidarr/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs

87 lines
2.7 KiB
C#
Raw Normal View History

using System;
using NLog.Config;
2013-08-30 16:18:12 +00:00
using NLog;
using NLog.Targets;
using NzbDrone.Common.Extensions;
2013-08-30 16:18:12 +00:00
using NzbDrone.Core.Lifecycle;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
2013-08-30 16:18:12 +00:00
namespace NzbDrone.Core.ProgressMessaging
{
2013-09-11 06:33:47 +00:00
public class ProgressMessageTarget : Target, IHandle<ApplicationStartedEvent>
2013-08-30 16:18:12 +00:00
{
private readonly IEventAggregator _eventAggregator;
private readonly IManageCommandQueue _commandQueueManager;
2013-09-11 06:33:47 +00:00
private static LoggingRule _rule;
2013-08-30 16:18:12 +00:00
private const string REENTRY_LOCK = "ProgressMessagingLock";
public ProgressMessageTarget(IEventAggregator eventAggregator, IManageCommandQueue commandQueueManager)
2013-08-30 16:18:12 +00:00
{
_eventAggregator = eventAggregator;
_commandQueueManager = commandQueueManager;
2013-08-30 16:18:12 +00:00
}
2013-09-11 06:33:47 +00:00
protected override void Write(LogEventInfo logEvent)
2013-08-30 16:18:12 +00:00
{
if (!ReentryPreventionCheck()) return;
2013-09-11 06:33:47 +00:00
var command = GetCurrentCommand();
2013-08-30 16:18:12 +00:00
2013-09-11 06:33:47 +00:00
if (IsClientMessage(logEvent, command))
{
_commandQueueManager.SetMessage(command, logEvent.FormattedMessage);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
2013-09-11 06:33:47 +00:00
}
MappedDiagnosticsContext.Remove(REENTRY_LOCK);
2013-08-30 16:18:12 +00:00
}
private CommandModel GetCurrentCommand()
2013-08-30 16:18:12 +00:00
{
var commandId = MappedDiagnosticsContext.Get("CommandId");
if (String.IsNullOrWhiteSpace(commandId))
2013-08-30 16:18:12 +00:00
{
2013-09-11 06:33:47 +00:00
return null;
2013-08-30 16:18:12 +00:00
}
return _commandQueueManager.Get(Convert.ToInt32(commandId));
2013-08-30 16:18:12 +00:00
}
private bool IsClientMessage(LogEventInfo logEvent, CommandModel command)
2013-08-30 16:18:12 +00:00
{
if (command == null || !command.Body.SendUpdatesToClient)
2013-08-30 16:18:12 +00:00
{
2013-09-11 06:33:47 +00:00
return false;
2013-08-30 16:18:12 +00:00
}
2013-09-11 06:33:47 +00:00
return logEvent.Properties.ContainsKey("Status");
2013-08-30 16:18:12 +00:00
}
private bool ReentryPreventionCheck()
{
var reentryLock = MappedDiagnosticsContext.Get(REENTRY_LOCK);
var commandId = MappedDiagnosticsContext.Get("CommandId");
if (reentryLock.IsNullOrWhiteSpace() || reentryLock != commandId)
{
MappedDiagnosticsContext.Set(REENTRY_LOCK, MappedDiagnosticsContext.Get("CommandId"));
return true;
}
return false;
}
2013-09-11 06:33:47 +00:00
public void Handle(ApplicationStartedEvent message)
2013-08-30 16:18:12 +00:00
{
2013-09-11 06:33:47 +00:00
_rule = new LoggingRule("*", LogLevel.Trace, this);
LogManager.Configuration.AddTarget("ProgressMessagingLogger", this);
LogManager.Configuration.LoggingRules.Add(_rule);
LogManager.ReconfigExistingLoggers();
2013-08-30 16:18:12 +00:00
}
}
}