You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs

66 lines
2.0 KiB

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