From fdafa596832eae13cebcf5bbe5fa867f7ba068f0 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Thu, 21 Feb 2013 20:26:35 -0500 Subject: Removed System.Windows.Forms dependancy from Common. Almost done removing NLog dependancy. --- MediaBrowser.Common/IO/FileSystem.cs | 5 +- MediaBrowser.Common/Kernel/BaseKernel.cs | 82 +--- .../Kernel/BasePeriodicWebSocketListener.cs | 5 +- MediaBrowser.Common/Kernel/IApplicationHost.cs | 19 + MediaBrowser.Common/Kernel/IKernel.cs | 9 +- MediaBrowser.Common/Logging/LogHelper.cs | 90 ----- MediaBrowser.Common/Logging/LogManager.cs | 20 - MediaBrowser.Common/Logging/LogWindow.xaml | 8 - MediaBrowser.Common/Logging/LogWindow.xaml.cs | 128 ------- MediaBrowser.Common/Logging/NLogger.cs | 201 ---------- MediaBrowser.Common/Logging/WindowTraceListener.cs | 75 ---- MediaBrowser.Common/MediaBrowser.Common.csproj | 18 +- MediaBrowser.Common/Net/AlchemyWebSocket.cs | 1 - MediaBrowser.Common/Net/BaseRestService.cs | 12 +- MediaBrowser.Common/Net/Handlers/BaseHandler.cs | 27 +- MediaBrowser.Common/Net/HttpServer.cs | 4 +- MediaBrowser.Common/Net/WebSocketConnection.cs | 3 +- MediaBrowser.Common/Plugins/BasePlugin.cs | 1 - .../ScheduledTasks/BaseScheduledTask.cs | 6 +- .../ScheduledTasks/IScheduledTask.cs | 4 +- .../ScheduledTasks/Tasks/SystemUpdateTask.cs | 1 - MediaBrowser.Common/Serialization/XmlSerializer.cs | 3 +- MediaBrowser.Common/UI/BaseApplication.cs | 412 --------------------- 23 files changed, 67 insertions(+), 1067 deletions(-) create mode 100644 MediaBrowser.Common/Kernel/IApplicationHost.cs delete mode 100644 MediaBrowser.Common/Logging/LogHelper.cs delete mode 100644 MediaBrowser.Common/Logging/LogManager.cs delete mode 100644 MediaBrowser.Common/Logging/LogWindow.xaml delete mode 100644 MediaBrowser.Common/Logging/LogWindow.xaml.cs delete mode 100644 MediaBrowser.Common/Logging/NLogger.cs delete mode 100644 MediaBrowser.Common/Logging/WindowTraceListener.cs delete mode 100644 MediaBrowser.Common/UI/BaseApplication.cs (limited to 'MediaBrowser.Common') diff --git a/MediaBrowser.Common/IO/FileSystem.cs b/MediaBrowser.Common/IO/FileSystem.cs index fde46dd5ba..3d65167bd7 100644 --- a/MediaBrowser.Common/IO/FileSystem.cs +++ b/MediaBrowser.Common/IO/FileSystem.cs @@ -1,8 +1,7 @@ -using System.Collections.Specialized; -using MediaBrowser.Common.Logging; -using MediaBrowser.Common.Win32; +using MediaBrowser.Common.Win32; using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Runtime.InteropServices; using System.Text; diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 2b2063b550..a4ac707494 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -9,9 +9,6 @@ using MediaBrowser.Common.Serialization; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Logging; using MediaBrowser.Model.System; -using NLog; -using NLog.Config; -using NLog.Targets; using System; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -40,11 +37,6 @@ namespace MediaBrowser.Common.Kernel /// public event EventHandler HasPendingRestartChanged; - /// - /// Notifiies the containing application that a restart has been requested - /// - public event EventHandler ApplicationRestartRequested; - #region ConfigurationUpdated Event /// /// Occurs when [configuration updated]. @@ -341,6 +333,12 @@ namespace MediaBrowser.Common.Kernel /// The logger. protected ILogger Logger { get; private set; } + /// + /// Gets or sets the application host. + /// + /// The application host. + protected IApplicationHost ApplicationHost { get; private set; } + /// /// Gets the assemblies. /// @@ -350,10 +348,17 @@ namespace MediaBrowser.Common.Kernel /// /// Initializes a new instance of the class. /// + /// The app host. /// The iso manager. /// The logger. - protected BaseKernel(IIsoManager isoManager, ILogger logger) + /// isoManager + protected BaseKernel(IApplicationHost appHost, IIsoManager isoManager, ILogger logger) { + if (appHost == null) + { + throw new ArgumentNullException("appHost"); + } + if (isoManager == null) { throw new ArgumentNullException("isoManager"); @@ -363,7 +368,8 @@ namespace MediaBrowser.Common.Kernel { throw new ArgumentNullException("logger"); } - + + ApplicationHost = appHost; IsoManager = isoManager; Logger = logger; } @@ -440,42 +446,11 @@ namespace MediaBrowser.Common.Kernel /// public void ReloadLogger() { - DisposeLogger(); - - LogFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, KernelContext + "-" + DateTime.Now.Ticks + ".log"); - - var logFile = new FileTarget(); - - logFile.FileName = LogFilePath; - logFile.Layout = "${longdate}, ${level}, ${logger}, ${message}"; - - AddLogTarget(logFile, "ApplicationLogFile"); - + ApplicationHost.ReloadLogger(); + OnLoggerLoaded(); } - /// - /// Adds the log target. - /// - /// The target. - /// The name. - private void AddLogTarget(Target target, string name) - { - var config = LogManager.Configuration; - - config.RemoveTarget(name); - - target.Name = name; - config.AddTarget(name, target); - - var level = Configuration.EnableDebugLevelLogging ? LogLevel.Debug : LogLevel.Info; - - var rule = new LoggingRule("*", level, target); - config.LoggingRules.Add(rule); - - LogManager.Configuration = config; - } - /// /// Uses MEF to locate plugins /// Subclasses can use this to locate types within plugins @@ -588,7 +563,7 @@ namespace MediaBrowser.Common.Kernel foreach (var task in ScheduledTasks) { - task.Initialize(this); + task.Initialize(this, Logger); } // Start-up each plugin @@ -707,23 +682,6 @@ namespace MediaBrowser.Common.Kernel } } - /// - /// Disposes all logger resources - /// - private void DisposeLogger() - { - // Dispose all current loggers - var listeners = Trace.Listeners.OfType().ToList(); - - Trace.Listeners.Clear(); - - foreach (var listener in listeners) - { - listener.Dispose(); - } - - } - /// /// Gets the current application version /// @@ -759,7 +717,7 @@ namespace MediaBrowser.Common.Kernel { Logger.Info("Restarting the application"); - EventHelper.QueueEventIfNotNull(ApplicationRestartRequested, this, EventArgs.Empty, Logger); + ApplicationHost.Restart(); } /// diff --git a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs index f431c5463a..11ffa8d6c0 100644 --- a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs +++ b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs @@ -1,12 +1,11 @@ -using MediaBrowser.Common.Logging; -using MediaBrowser.Common.Net; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Model.Logging; namespace MediaBrowser.Common.Kernel { diff --git a/MediaBrowser.Common/Kernel/IApplicationHost.cs b/MediaBrowser.Common/Kernel/IApplicationHost.cs new file mode 100644 index 0000000000..c1b63c2613 --- /dev/null +++ b/MediaBrowser.Common/Kernel/IApplicationHost.cs @@ -0,0 +1,19 @@ + +namespace MediaBrowser.Common.Kernel +{ + /// + /// An interface to be implemented by the applications hosting a kernel + /// + public interface IApplicationHost + { + /// + /// Restarts this instance. + /// + void Restart(); + + /// + /// Reloads the logger. + /// + void ReloadLogger(); + } +} diff --git a/MediaBrowser.Common/Kernel/IKernel.cs b/MediaBrowser.Common/Kernel/IKernel.cs index 5358f3ab09..c0e650fc77 100644 --- a/MediaBrowser.Common/Kernel/IKernel.cs +++ b/MediaBrowser.Common/Kernel/IKernel.cs @@ -1,6 +1,4 @@ -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net; -using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Common.Serialization; @@ -18,11 +16,6 @@ namespace MediaBrowser.Common.Kernel /// public interface IKernel { - /// - /// Occurs when [application restart requested]. - /// - event EventHandler ApplicationRestartRequested; - /// /// Gets the application paths. /// diff --git a/MediaBrowser.Common/Logging/LogHelper.cs b/MediaBrowser.Common/Logging/LogHelper.cs deleted file mode 100644 index 8b638a28e8..0000000000 --- a/MediaBrowser.Common/Logging/LogHelper.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Text; - -namespace MediaBrowser.Common.Logging -{ - /// - /// Class LogHelper - /// - public static class LogHelper - { - /// - /// Gets the log message. - /// - /// The exception. - /// StringBuilder. - public static StringBuilder GetLogMessage(Exception exception) - { - var messageText = new StringBuilder(); - - messageText.AppendLine(exception.Message); - - messageText.AppendLine(exception.GetType().FullName); - - LogExceptionData(messageText, exception); - - messageText.AppendLine(exception.StackTrace ?? "No Stack Trace Available"); - - // Log the InnerExceptions, if any - AppendInnerExceptions(messageText, exception); - - messageText.AppendLine(string.Empty); - - return messageText; - } - - /// - /// Appends the inner exceptions. - /// - /// The message text. - /// The e. - private static void AppendInnerExceptions(StringBuilder messageText, Exception e) - { - var aggregate = e as AggregateException; - - if (aggregate != null && aggregate.InnerExceptions != null) - { - foreach (var ex in aggregate.InnerExceptions) - { - AppendInnerException(messageText, ex); - } - } - - else if (e.InnerException != null) - { - AppendInnerException(messageText, e.InnerException); - } - } - - /// - /// Appends the inner exception. - /// - /// The message text. - /// The e. - private static void AppendInnerException(StringBuilder messageText, Exception e) - { - messageText.AppendLine("InnerException: " + e.GetType().FullName); - messageText.AppendLine(e.Message); - - LogExceptionData(messageText, e); - - if (e.StackTrace != null) - { - messageText.AppendLine(e.StackTrace); - } - } - - /// - /// Logs the exception data. - /// - /// The message text. - /// The e. - private static void LogExceptionData(StringBuilder messageText, Exception e) - { - foreach (var key in e.Data.Keys) - { - messageText.AppendLine(key + ": " + e.Data[key]); - } - } - } -} diff --git a/MediaBrowser.Common/Logging/LogManager.cs b/MediaBrowser.Common/Logging/LogManager.cs deleted file mode 100644 index 825f04440f..0000000000 --- a/MediaBrowser.Common/Logging/LogManager.cs +++ /dev/null @@ -1,20 +0,0 @@ -using MediaBrowser.Model.Logging; - -namespace MediaBrowser.Common.Logging -{ - /// - /// Class LogManager - /// - public static class LogManager - { - /// - /// Gets the logger. - /// - /// The name. - /// ILogger. - public static ILogger GetLogger(string name) - { - return new NLogger(name); - } - } -} diff --git a/MediaBrowser.Common/Logging/LogWindow.xaml b/MediaBrowser.Common/Logging/LogWindow.xaml deleted file mode 100644 index 726d3c968e..0000000000 --- a/MediaBrowser.Common/Logging/LogWindow.xaml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - diff --git a/MediaBrowser.Common/Logging/LogWindow.xaml.cs b/MediaBrowser.Common/Logging/LogWindow.xaml.cs deleted file mode 100644 index 3dc11db5b2..0000000000 --- a/MediaBrowser.Common/Logging/LogWindow.xaml.cs +++ /dev/null @@ -1,128 +0,0 @@ -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.Common.Logging -{ - /// - /// Interaction logic for LogWindow.xaml - /// - public partial class LogWindow : Window - { - /// - /// The _ui thread - /// - private readonly TaskScheduler _uiThread; - /// - /// The _kernel - /// - private readonly IKernel _kernel; - - /// - /// Initializes a new instance of the class. - /// - /// The kernel. - public LogWindow(IKernel kernel) - { - InitializeComponent(); - _uiThread = TaskScheduler.FromCurrentSynchronizationContext(); - _kernel = kernel; - - 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) - { - var target = new TraceTarget - { - Layout = "${longdate}, ${level}, ${logger}, ${message}" - }; - - AddLogTarget(target, "LogWindowTraceTarget"); - } - - /// - /// Raises the event. - /// - /// A that contains the event data. - protected override void OnClosing(CancelEventArgs e) - { - base.OnClosing(e); - - RemoveLogTarget("LogWindowTraceTarget"); - } - - /// - /// Logs the message. - /// - /// The MSG. - public async void LogMessage(string msg) - { - await Task.Factory.StartNew(() => 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; - - 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; - } - - /// - /// Removes the log target. - /// - /// The name. - private void RemoveLogTarget(string name) - { - var config = NLog.LogManager.Configuration; - - config.RemoveTarget(name); - - NLog.LogManager.Configuration = config; - } - - /// - /// Shuts down. - /// - public async void ShutDown() - { - await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread); - } - - } - -} diff --git a/MediaBrowser.Common/Logging/NLogger.cs b/MediaBrowser.Common/Logging/NLogger.cs deleted file mode 100644 index 584c5616bf..0000000000 --- a/MediaBrowser.Common/Logging/NLogger.cs +++ /dev/null @@ -1,201 +0,0 @@ -using MediaBrowser.Model.Logging; -using System; -using System.Text; - -namespace MediaBrowser.Common.Logging -{ - /// - /// Class NLogger - /// - internal class NLogger : ILogger - { - /// - /// The _logger - /// - private readonly NLog.Logger _logger; - - /// - /// The _lock object - /// - private static readonly object LockObject = new object(); - - /// - /// Initializes a new instance of the class. - /// - /// The name. - public NLogger(string name) - { - lock (LockObject) - { - _logger = NLog.LogManager.GetLogger(name); - } - } - - /// - /// Infoes the specified message. - /// - /// The message. - /// The param list. - public void Info(string message, params object[] paramList) - { - _logger.Info(message, paramList); - } - - /// - /// Errors the specified message. - /// - /// The message. - /// The param list. - public void Error(string message, params object[] paramList) - { - _logger.Error(message, paramList); - } - - /// - /// Warns the specified message. - /// - /// The message. - /// The param list. - public void Warn(string message, params object[] paramList) - { - _logger.Warn(message, paramList); - } - - /// - /// Debugs the specified message. - /// - /// The message. - /// The param list. - public void Debug(string message, params object[] paramList) - { - _logger.Debug(message, paramList); - } - - /// - /// Logs the exception. - /// - /// The message. - /// The exception. - /// The param list. - /// - public void ErrorException(string message, Exception exception, params object[] paramList) - { - LogException(LogSeverity.Error, message, exception, paramList); - } - - /// - /// Logs the exception. - /// - /// The level. - /// The message. - /// The exception. - /// The param list. - private void LogException(LogSeverity level, string message, Exception exception, params object[] paramList) - { - message = FormatMessage(message, paramList).Replace(Environment.NewLine, ". "); - - var messageText = LogHelper.GetLogMessage(exception); - - LogMultiline(message, level, messageText); - } - - /// - /// Formats the message. - /// - /// The message. - /// The param list. - /// System.String. - private static string FormatMessage(string message, params object[] paramList) - { - if (paramList != null) - { - for (var i = 0; i < paramList.Length; i++) - { - message = message.Replace("{" + i + "}", paramList[i].ToString()); - } - } - - return message; - } - - /// - /// Logs the multiline. - /// - /// The message. - /// The severity. - /// Content of the additional. - public void LogMultiline(string message, LogSeverity severity, StringBuilder additionalContent) - { - additionalContent.Insert(0, message + Environment.NewLine); - - const char tabChar = '\t'; - - var text = additionalContent.ToString() - .Replace(Environment.NewLine, Environment.NewLine + tabChar) - .TrimEnd(tabChar); - - if (text.EndsWith(Environment.NewLine)) - { - text = text.Substring(0, text.LastIndexOf(Environment.NewLine, StringComparison.OrdinalIgnoreCase)); - } - - _logger.Log(GetLogLevel(severity), text); - } - - /// - /// Gets the log level. - /// - /// The severity. - /// NLog.LogLevel. - private NLog.LogLevel GetLogLevel(LogSeverity severity) - { - switch (severity) - { - case LogSeverity.Debug: - return NLog.LogLevel.Debug; - case LogSeverity.Error: - return NLog.LogLevel.Error; - case LogSeverity.Warn: - return NLog.LogLevel.Warn; - case LogSeverity.Fatal: - return NLog.LogLevel.Fatal; - case LogSeverity.Info: - return NLog.LogLevel.Info; - default: - throw new ArgumentException("Unknown LogSeverity: " + severity.ToString()); - } - } - - /// - /// Logs the specified severity. - /// - /// The severity. - /// The message. - /// The param list. - public void Log(LogSeverity severity, string message, params object[] paramList) - { - _logger.Log(GetLogLevel(severity), message, paramList); - } - - /// - /// Fatals the specified message. - /// - /// The message. - /// The param list. - public void Fatal(string message, params object[] paramList) - { - _logger.Fatal(message, paramList); - } - - /// - /// Fatals the exception. - /// - /// The message. - /// The exception. - /// The param list. - public void FatalException(string message, Exception exception, params object[] paramList) - { - LogException(LogSeverity.Fatal, message, exception, paramList); - } - } -} diff --git a/MediaBrowser.Common/Logging/WindowTraceListener.cs b/MediaBrowser.Common/Logging/WindowTraceListener.cs deleted file mode 100644 index 046dedf86e..0000000000 --- a/MediaBrowser.Common/Logging/WindowTraceListener.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.Diagnostics; - -namespace MediaBrowser.Common.Logging -{ - /// - /// Class WindowTraceListener - /// - public class WindowTraceListener : DefaultTraceListener - { - /// - /// The _window - /// - private readonly LogWindow _window; - /// - /// Initializes a new instance of the class. - /// - /// The window. - public WindowTraceListener(LogWindow window) - { - _window = window; - _window.Show(); - Name = "MBLogWindow"; - } - - /// - /// Writes the value of the object's method to the listener you create when you implement the class. - /// - /// An whose fully qualified class name you want to write. - public override void Write(object o) - { - var str = o as string; - if (str != null) - Write(str); - else - base.Write(o); - } - - /// - /// Writes the output to the OutputDebugString function and to the method. - /// - /// The message to write to OutputDebugString and . - /// - /// - /// - /// - public override void Write(string message) - { - _window.LogMessage(message); - } - - /// - /// Writes the output to the OutputDebugString function and to the method, followed by a carriage return and line feed (\r\n). - /// - /// The message to write to OutputDebugString and . - /// - /// - /// - /// - public override void WriteLine(string message) - { - Write(message+"\n"); - } - - /// - /// Releases the unmanaged resources used by the and optionally releases the managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected override void Dispose(bool disposing) - { - if (_window != null) - _window.ShutDown(); - base.Dispose(disposing); - } - } -} diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index b80cd8e84d..b806df856b 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -114,8 +114,6 @@ - - @@ -142,16 +140,10 @@ + - - - - LogWindow.xaml - - - @@ -201,7 +193,6 @@ - @@ -228,12 +219,6 @@ - - - Designer - MSBuild:Compile - - @@ -251,6 +236,7 @@ +