1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 10:07:10 +00:00
Lidarr/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs

68 lines
2 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;
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
}
}
}