jellyfin-server/MediaBrowser.ServerApplication/Logging/LogWindow.xaml.cs

115 lines
3.5 KiB
C#
Raw Normal View History

2013-03-04 16:31:33 +00:00
using MediaBrowser.Common.Implementations.Logging;
using MediaBrowser.Model.Logging;
using NLog;
2013-02-21 01:33:05 +00:00
using NLog.Config;
using NLog.Targets;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace MediaBrowser.ServerApplication.Logging
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Interaction logic for LogWindow.xaml
/// </summary>
public partial class LogWindow : Window
{
/// <summary>
/// The _ui thread
/// </summary>
private readonly TaskScheduler _uiThread;
2013-03-04 16:31:33 +00:00
private readonly ILogManager _logManager;
2013-02-21 01:33:05 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="LogWindow" /> class.
/// </summary>
/// <param name="kernel">The kernel.</param>
2013-03-04 16:31:33 +00:00
public LogWindow(ILogManager logManager)
2013-02-21 01:33:05 +00:00
{
InitializeComponent();
_uiThread = TaskScheduler.FromCurrentSynchronizationContext();
2013-03-04 16:31:33 +00:00
_logManager = logManager;
2013-02-21 01:33:05 +00:00
Loaded += LogWindow_Loaded;
}
/// <summary>
/// Handles the Loaded event of the LogWindow control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
void LogWindow_Loaded(object sender, RoutedEventArgs e)
{
2013-03-05 18:11:25 +00:00
((NlogManager)_logManager).RemoveTarget("LogWindowTraceTarget");
((NlogManager)_logManager).AddLogTarget(new TraceTarget
2013-02-21 01:33:05 +00:00
{
2013-03-05 18:11:25 +00:00
Layout = "${longdate}, ${level}, ${logger}, ${message}",
Name = "LogWindowTraceTarget"
2013-02-21 01:33:05 +00:00
2013-03-05 18:11:25 +00:00
}, LogSeverity.Debug);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Raises the <see cref="E:System.Windows.Window.Closing" /> event.
/// </summary>
/// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
2013-03-04 16:31:33 +00:00
((NlogManager) _logManager).RemoveTarget("LogWindowTraceTarget");
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Logs the message.
/// </summary>
/// <param name="msg">The MSG.</param>
public async void LogMessage(string msg)
{
await Task.Factory.StartNew(() => lbxLogData.Items.Insert(0, msg.TrimEnd('\n')), CancellationToken.None, TaskCreationOptions.None, _uiThread);
}
/// <summary>
/// The log layout
/// </summary>
/// <value>The log layout.</value>
public string LogLayout
{
get { return "${longdate}, ${level}, ${logger}, ${message}"; }
}
/// <summary>
/// Adds the log target.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="name">The name.</param>
private void AddLogTarget(Target target, string name)
{
var config = NLog.LogManager.Configuration;
target.Name = name;
config.AddTarget(name, target);
2013-03-04 05:43:06 +00:00
var level = LogLevel.Debug;
2013-02-21 01:33:05 +00:00
var rule = new LoggingRule("*", level, target);
config.LoggingRules.Add(rule);
NLog.LogManager.Configuration = config;
}
/// <summary>
/// Shuts down.
/// </summary>
public async void ShutDown()
{
await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread);
}
}
}