diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-21 20:26:35 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-21 20:26:35 -0500 |
| commit | fdafa596832eae13cebcf5bbe5fa867f7ba068f0 (patch) | |
| tree | eee891c8f11564d4b14868d11f4758f243c112ce /MediaBrowser.ServerApplication/Logging | |
| parent | 931c0ea455161b8ee00005a0ffd1f8afab41f7bb (diff) | |
Removed System.Windows.Forms dependancy from Common. Almost done removing NLog dependancy.
Diffstat (limited to 'MediaBrowser.ServerApplication/Logging')
3 files changed, 211 insertions, 0 deletions
diff --git a/MediaBrowser.ServerApplication/Logging/LogWindow.xaml b/MediaBrowser.ServerApplication/Logging/LogWindow.xaml new file mode 100644 index 000000000..d50b55454 --- /dev/null +++ b/MediaBrowser.ServerApplication/Logging/LogWindow.xaml @@ -0,0 +1,8 @@ +<Window x:Class="MediaBrowser.ServerApplication.Logging.LogWindow" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + Title="Media Browser Log" Height="300" Width="968"> + <Grid> + <ListBox x:Name="lbxLogData" Margin="10,10,0,0"/> + </Grid> +</Window> diff --git a/MediaBrowser.ServerApplication/Logging/LogWindow.xaml.cs b/MediaBrowser.ServerApplication/Logging/LogWindow.xaml.cs new file mode 100644 index 000000000..fca978486 --- /dev/null +++ b/MediaBrowser.ServerApplication/Logging/LogWindow.xaml.cs @@ -0,0 +1,128 @@ +using MediaBrowser.Common.Kernel; +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 +{ + /// <summary> + /// Interaction logic for LogWindow.xaml + /// </summary> + public partial class LogWindow : Window + { + /// <summary> + /// The _ui thread + /// </summary> + private readonly TaskScheduler _uiThread; + /// <summary> + /// The _kernel + /// </summary> + private readonly IKernel _kernel; + + /// <summary> + /// Initializes a new instance of the <see cref="LogWindow" /> class. + /// </summary> + /// <param name="kernel">The kernel.</param> + public LogWindow(IKernel kernel) + { + InitializeComponent(); + _uiThread = TaskScheduler.FromCurrentSynchronizationContext(); + _kernel = kernel; + + 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) + { + var target = new TraceTarget + { + Layout = "${longdate}, ${level}, ${logger}, ${message}" + }; + + AddLogTarget(target, "LogWindowTraceTarget"); + } + + /// <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); + + RemoveLogTarget("LogWindowTraceTarget"); + } + + /// <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; + + config.RemoveTarget(name); + + target.Name = name; + config.AddTarget(name, target); + + var level = _kernel.Configuration.EnableDebugLevelLogging ? LogLevel.Debug : LogLevel.Info; + + var rule = new LoggingRule("*", level, target); + config.LoggingRules.Add(rule); + + NLog.LogManager.Configuration = config; + } + + /// <summary> + /// Removes the log target. + /// </summary> + /// <param name="name">The name.</param> + private void RemoveLogTarget(string name) + { + var config = NLog.LogManager.Configuration; + + config.RemoveTarget(name); + + NLog.LogManager.Configuration = config; + } + + /// <summary> + /// Shuts down. + /// </summary> + public async void ShutDown() + { + await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread); + } + + } + +} diff --git a/MediaBrowser.ServerApplication/Logging/WindowTraceListener.cs b/MediaBrowser.ServerApplication/Logging/WindowTraceListener.cs new file mode 100644 index 000000000..10d6ef812 --- /dev/null +++ b/MediaBrowser.ServerApplication/Logging/WindowTraceListener.cs @@ -0,0 +1,75 @@ +using System.Diagnostics; + +namespace MediaBrowser.ServerApplication.Logging +{ + /// <summary> + /// Class WindowTraceListener + /// </summary> + public class WindowTraceListener : DefaultTraceListener + { + /// <summary> + /// The _window + /// </summary> + private readonly LogWindow _window; + /// <summary> + /// Initializes a new instance of the <see cref="WindowTraceListener" /> class. + /// </summary> + /// <param name="window">The window.</param> + public WindowTraceListener(LogWindow window) + { + _window = window; + _window.Show(); + Name = "MBLogWindow"; + } + + /// <summary> + /// Writes the value of the object's <see cref="M:System.Object.ToString" /> method to the listener you create when you implement the <see cref="T:System.Diagnostics.TraceListener" /> class. + /// </summary> + /// <param name="o">An <see cref="T:System.Object" /> whose fully qualified class name you want to write.</param> + public override void Write(object o) + { + var str = o as string; + if (str != null) + Write(str); + else + base.Write(o); + } + + /// <summary> + /// Writes the output to the OutputDebugString function and to the <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)" /> method. + /// </summary> + /// <param name="message">The message to write to OutputDebugString and <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)" />.</param> + /// <PermissionSet> + /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> + /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence" /> + /// </PermissionSet> + public override void Write(string message) + { + _window.LogMessage(message); + } + + /// <summary> + /// Writes the output to the OutputDebugString function and to the <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)" /> method, followed by a carriage return and line feed (\r\n). + /// </summary> + /// <param name="message">The message to write to OutputDebugString and <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)" />.</param> + /// <PermissionSet> + /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> + /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence" /> + /// </PermissionSet> + public override void WriteLine(string message) + { + Write(message+"\n"); + } + + /// <summary> + /// Releases the unmanaged resources used by the <see cref="T:System.Diagnostics.TraceListener" /> and optionally releases the managed resources. + /// </summary> + /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> + protected override void Dispose(bool disposing) + { + if (_window != null) + _window.ShutDown(); + base.Dispose(disposing); + } + } +} |
