using MediaBrowser.Common.Implementations.Logging;
using MediaBrowser.Model.Logging;
using NLog;
using NLog.Config;
using NLog.Targets;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace MediaBrowser.ServerApplication.Logging
{
///
/// Interaction logic for LogWindow.xaml
///
public partial class LogWindow : Window
{
///
/// The _ui thread
///
private readonly TaskScheduler _uiThread;
private readonly ILogManager _logManager;
///
/// Initializes a new instance of the class.
///
/// The kernel.
public LogWindow(ILogManager logManager)
{
InitializeComponent();
_uiThread = TaskScheduler.FromCurrentSynchronizationContext();
_logManager = logManager;
Loaded += LogWindow_Loaded;
}
///
/// Handles the Loaded event of the LogWindow control.
///
/// The source of the event.
/// The instance containing the event data.
void LogWindow_Loaded(object sender, RoutedEventArgs e)
{
((NlogManager)_logManager).RemoveTarget("LogWindowTraceTarget");
((NlogManager)_logManager).AddLogTarget(new TraceTarget
{
Layout = "${longdate}, ${level}, ${logger}, ${message}",
Name = "LogWindowTraceTarget"
}, LogSeverity.Debug);
}
///
/// Raises the event.
///
/// A that contains the event data.
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
((NlogManager) _logManager).RemoveTarget("LogWindowTraceTarget");
}
///
/// Logs the message.
///
/// The MSG.
public async void LogMessage(string msg)
{
await Task.Factory.StartNew(() =>
{
if (lbxLogData.Items.Count > 10000)
{
//I think the quickest and safest thing to do here is just clear it out
lbxLogData.Items.Clear();
}
lbxLogData.Items.Insert(0, msg.TrimEnd('\n'));
}, CancellationToken.None, TaskCreationOptions.None, _uiThread);
}
///
/// The log layout
///
/// The log layout.
public string LogLayout
{
get { return "${longdate}, ${level}, ${logger}, ${message}"; }
}
///
/// Adds the log target.
///
/// The target.
/// The name.
private void AddLogTarget(Target target, string name)
{
var config = NLog.LogManager.Configuration;
target.Name = name;
config.AddTarget(name, target);
var level = LogLevel.Debug;
var rule = new LoggingRule("*", level, target);
config.LoggingRules.Add(rule);
NLog.LogManager.Configuration = config;
}
///
/// Shuts down.
///
public async void ShutDown()
{
await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread);
}
}
}