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

62 lines
1.9 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
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
{
var command = ProgressMessageContext.CommandModel;
2013-08-30 16:18:12 +00:00
if (!IsClientMessage(logEvent, command)) return;
if (!ProgressMessageContext.LockReentrancy()) return;
try
2013-09-11 06:33:47 +00:00
{
_commandQueueManager.SetMessage(command, logEvent.FormattedMessage);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
2013-09-11 06:33:47 +00:00
}
finally
2013-08-30 16:18:12 +00:00
{
ProgressMessageContext.UnlockReentrancy();
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
}
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
}
}
}