1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs

68 lines
2.0 KiB
C#
Raw Normal View History

2013-09-11 09:33:47 +03:00
using NLog.Config;
2013-08-30 19:18:12 +03:00
using NLog;
using NLog.Targets;
using NzbDrone.Core.Lifecycle;
2013-09-11 09:33:47 +03:00
using NzbDrone.Core.Messaging;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Commands.Tracking;
using NzbDrone.Core.Messaging.Events;
2013-08-30 19:18:12 +03:00
namespace NzbDrone.Core.ProgressMessaging
{
2013-09-11 09:33:47 +03:00
public class ProgressMessageTarget : Target, IHandle<ApplicationStartedEvent>
2013-08-30 19:18:12 +03:00
{
private readonly IEventAggregator _eventAggregator;
2013-09-11 09:33:47 +03:00
private readonly ITrackCommands _trackCommands;
private static LoggingRule _rule;
2013-08-30 19:18:12 +03:00
public ProgressMessageTarget(IEventAggregator eventAggregator, ITrackCommands trackCommands)
2013-08-30 19:18:12 +03:00
{
_eventAggregator = eventAggregator;
2013-09-11 09:33:47 +03:00
_trackCommands = trackCommands;
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
protected override void Write(LogEventInfo logEvent)
2013-08-30 19:18:12 +03:00
{
2013-09-11 09:33:47 +03:00
var command = GetCurrentCommand();
2013-08-30 19:18:12 +03:00
2013-09-11 09:33:47 +03:00
if (IsClientMessage(logEvent, command))
{
command.SetMessage(logEvent.FormattedMessage);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
2013-09-11 09:33:47 +03:00
}
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
private Command GetCurrentCommand()
2013-08-30 19:18:12 +03:00
{
var commandId = MappedDiagnosticsContext.Get("CommandId");
2013-09-11 09:33:47 +03:00
if (string.IsNullOrWhiteSpace(commandId))
2013-08-30 19:18:12 +03:00
{
2013-09-11 09:33:47 +03:00
return null;
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
return _trackCommands.GetById(commandId);
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
private bool IsClientMessage(LogEventInfo logEvent, Command command)
2013-08-30 19:18:12 +03:00
{
2013-09-11 09:33:47 +03:00
if (command == null || !command.SendUpdatesToClient)
2013-08-30 19:18:12 +03:00
{
2013-09-11 09:33:47 +03:00
return false;
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
return logEvent.Properties.ContainsKey("Status");
2013-08-30 19:18:12 +03:00
}
2013-09-11 09:33:47 +03:00
public void Handle(ApplicationStartedEvent message)
2013-08-30 19:18:12 +03:00
{
2013-09-11 09:33:47 +03:00
_rule = new LoggingRule("*", LogLevel.Trace, this);
LogManager.Configuration.AddTarget("ProgressMessagingLogger", this);
LogManager.Configuration.LoggingRules.Add(_rule);
LogManager.ReconfigExistingLoggers();
2013-08-30 19:18:12 +03:00
}
}
}