using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Media; using NLog; using NLog.Targets; namespace Torch.Server { [Target("flowDocument")] public sealed class FlowDocumentTarget : TargetWithLayout { private FlowDocument _document = new FlowDocument { Background = new SolidColorBrush(Colors.Black) }; private readonly Paragraph _paragraph = new Paragraph(); public FlowDocument Document => _document; public FlowDocumentTarget() { _document.Blocks.Add(_paragraph); } /// protected override void Write(LogEventInfo logEvent) { _document.Dispatcher.BeginInvoke(() => { var message = $"{Layout.Render(logEvent)}\n"; _paragraph.Inlines.Add(new Run(message) {Foreground = LogLevelColors[logEvent.Level]}); }); } private static readonly Dictionary LogLevelColors = new Dictionary { [LogLevel.Trace] = new SolidColorBrush(Colors.DimGray), [LogLevel.Debug] = new SolidColorBrush(Colors.DarkGray), [LogLevel.Info] = new SolidColorBrush(Colors.White), [LogLevel.Warn] = new SolidColorBrush(Colors.Magenta), [LogLevel.Error] = new SolidColorBrush(Colors.Yellow), [LogLevel.Fatal] = new SolidColorBrush(Colors.Red), }; } }