Lidarr/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs

67 lines
2.0 KiB
C#
Raw Normal View History

2013-09-11 06:33:47 +00:00
using NLog.Config;
2013-08-30 16:18:12 +00:00
using NLog;
using NLog.Targets;
using NzbDrone.Core.Lifecycle;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Commands.Tracking;
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;
2013-09-11 06:33:47 +00:00
private readonly ITrackCommands _trackCommands;
private static LoggingRule _rule;
2013-08-30 16:18:12 +00:00
public ProgressMessageTarget(IEventAggregator eventAggregator, ITrackCommands trackCommands)
2013-08-30 16:18:12 +00:00
{
_eventAggregator = eventAggregator;
2013-09-11 06:33:47 +00:00
_trackCommands = trackCommands;
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
{
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))
{
command.SetMessage(logEvent.FormattedMessage);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
2013-09-11 06:33:47 +00:00
}
2013-08-30 16:18:12 +00:00
}
2013-09-11 06:33:47 +00:00
private Command GetCurrentCommand()
2013-08-30 16:18:12 +00:00
{
var commandId = MappedDiagnosticsContext.Get("CommandId");
2013-09-11 06:33:47 +00:00
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
}
2013-09-11 06:33:47 +00:00
return _trackCommands.GetById(commandId);
2013-08-30 16:18:12 +00:00
}
2013-09-11 06:33:47 +00:00
private bool IsClientMessage(LogEventInfo logEvent, Command command)
2013-08-30 16:18:12 +00:00
{
2013-09-11 06:33:47 +00:00
if (command == null || !command.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
}
}
}