From ab1065a567151fd45fcf4698cd7d18708b94e35f Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Thu, 21 Feb 2013 15:26:35 -0500 Subject: removed static logger --- .../Api/Logging/LogFileWebSocketListener.cs | 12 +++ .../ScheduledTasksWebSocketListener.cs | 14 +++- .../Api/SystemInfoWebSocketListener.cs | 12 +++ MediaBrowser.Common/Events/EventHelper.cs | 14 +++- MediaBrowser.Common/IO/FileSystem.cs | 2 - MediaBrowser.Common/Kernel/BaseKernel.cs | 6 +- .../Kernel/BasePeriodicWebSocketListener.cs | 26 ++++++- MediaBrowser.Common/Logging/Logger.cs | 89 ---------------------- MediaBrowser.Common/MediaBrowser.Common.csproj | 1 - MediaBrowser.Common/Plugins/BasePlugin.cs | 12 ++- MediaBrowser.Common/Plugins/IPlugin.cs | 3 +- MediaBrowser.Common/Serialization/XmlSerializer.cs | 13 ++-- 12 files changed, 91 insertions(+), 113 deletions(-) delete mode 100644 MediaBrowser.Common/Logging/Logger.cs (limited to 'MediaBrowser.Common') diff --git a/MediaBrowser.Common/Api/Logging/LogFileWebSocketListener.cs b/MediaBrowser.Common/Api/Logging/LogFileWebSocketListener.cs index 335b77e0f..e873facb1 100644 --- a/MediaBrowser.Common/Api/Logging/LogFileWebSocketListener.cs +++ b/MediaBrowser.Common/Api/Logging/LogFileWebSocketListener.cs @@ -6,6 +6,7 @@ using System.ComponentModel.Composition; using System.IO; using System.Linq; using System.Threading.Tasks; +using MediaBrowser.Model.Logging; namespace MediaBrowser.Common.Api.Logging { @@ -24,6 +25,17 @@ namespace MediaBrowser.Common.Api.Logging get { return "LogFile"; } } + /// + /// Initializes a new instance of the class. + /// + /// The logger. + [ImportingConstructor] + public LogFileWebSocketListener([Import("logger")] ILogger logger) + : base(logger) + { + + } + /// /// Initializes the specified kernel. /// diff --git a/MediaBrowser.Common/Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs b/MediaBrowser.Common/Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs index 097478af6..7e4596d9d 100644 --- a/MediaBrowser.Common/Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs +++ b/MediaBrowser.Common/Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs @@ -1,5 +1,6 @@ using MediaBrowser.Common.Kernel; using MediaBrowser.Common.ScheduledTasks; +using MediaBrowser.Model.Logging; using MediaBrowser.Model.Tasks; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -22,7 +23,18 @@ namespace MediaBrowser.Common.Api.ScheduledTasks { get { return "ScheduledTasksInfo"; } } - + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + [ImportingConstructor] + public ScheduledTasksWebSocketListener([Import("logger")] ILogger logger) + : base(logger) + { + + } + /// /// Gets the data to send. /// diff --git a/MediaBrowser.Common/Api/SystemInfoWebSocketListener.cs b/MediaBrowser.Common/Api/SystemInfoWebSocketListener.cs index d2e404beb..a216937ed 100644 --- a/MediaBrowser.Common/Api/SystemInfoWebSocketListener.cs +++ b/MediaBrowser.Common/Api/SystemInfoWebSocketListener.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Kernel; +using MediaBrowser.Model.Logging; using System.ComponentModel.Composition; using System.Threading.Tasks; @@ -19,6 +20,17 @@ namespace MediaBrowser.Common.Api get { return "SystemInfo"; } } + /// + /// Initializes a new instance of the class. + /// + /// The logger. + [ImportingConstructor] + public SystemInfoWebSocketListener([Import("logger")] ILogger logger) + : base(logger) + { + + } + /// /// Gets the data to send. /// diff --git a/MediaBrowser.Common/Events/EventHelper.cs b/MediaBrowser.Common/Events/EventHelper.cs index bd2b1156e..6ed5ec591 100644 --- a/MediaBrowser.Common/Events/EventHelper.cs +++ b/MediaBrowser.Common/Events/EventHelper.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Logging; +using MediaBrowser.Model.Logging; using System; using System.Threading.Tasks; @@ -9,6 +10,11 @@ namespace MediaBrowser.Common.Events /// public static class EventHelper { + /// + /// The logger + /// + private static readonly ILogger Logger = LogManager.GetLogger("EventHelper"); + /// /// Fires the event. /// @@ -27,7 +33,7 @@ namespace MediaBrowser.Common.Events } catch (Exception ex) { - Logger.LogException("Error in event handler", ex); + Logger.ErrorException("Error in event handler", ex); } }); } @@ -52,7 +58,7 @@ namespace MediaBrowser.Common.Events } catch (Exception ex) { - Logger.LogException("Error in event handler", ex); + Logger.ErrorException("Error in event handler", ex); } }); } @@ -74,7 +80,7 @@ namespace MediaBrowser.Common.Events } catch (Exception ex) { - Logger.LogException("Error in event handler", ex); + Logger.ErrorException("Error in event handler", ex); } } } @@ -96,7 +102,7 @@ namespace MediaBrowser.Common.Events } catch (Exception ex) { - Logger.LogException("Error in event handler", ex); + Logger.ErrorException("Error in event handler", ex); } } } diff --git a/MediaBrowser.Common/IO/FileSystem.cs b/MediaBrowser.Common/IO/FileSystem.cs index e1abe7833..fde46dd5b 100644 --- a/MediaBrowser.Common/IO/FileSystem.cs +++ b/MediaBrowser.Common/IO/FileSystem.cs @@ -38,8 +38,6 @@ namespace MediaBrowser.Common.IO { if (!path.EndsWith("*", StringComparison.OrdinalIgnoreCase)) { - Logger.LogInfo("Handle came back invalid for {0}. This might be a network share. Since this is a directory we'll try appending " + Path.DirectorySeparatorChar + "*.", path); - NativeMethods.FindClose(handle); handle = NativeMethods.FindFirstFileEx(Path.Combine(path, "*"), FINDEX_INFO_LEVELS.FindExInfoBasic, out data, diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index a4a1d5b4d..fe0d619c4 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -143,7 +143,7 @@ namespace MediaBrowser.Common.Kernel get { // Lazy load - LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration(ApplicationPaths.SystemConfigurationFilePath)); + LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration(ApplicationPaths.SystemConfigurationFilePath, Logger)); return _configuration; } protected set @@ -441,8 +441,6 @@ namespace MediaBrowser.Common.Kernel AddLogTarget(logFile, "ApplicationLogFile"); - Logging.Logger.LoggerInstance = Logging.LogManager.GetLogger("App"); - OnLoggerLoaded(); } @@ -590,7 +588,7 @@ namespace MediaBrowser.Common.Kernel try { - plugin.Initialize(this); + plugin.Initialize(this, Logging.LogManager.GetLogger(plugin.GetType().Name)); Logger.Info("{0} {1} initialized.", plugin.Name, plugin.Version); } diff --git a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs index 7db1ca36b..f431c5463 100644 --- a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs +++ b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Logging; namespace MediaBrowser.Common.Kernel { @@ -38,6 +39,25 @@ namespace MediaBrowser.Common.Kernel /// Task{`1}. protected abstract Task GetDataToSend(TStateType state); + /// + /// The logger + /// + protected ILogger Logger; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + protected BasePeriodicWebSocketListener(ILogger logger) + { + if (logger == null) + { + throw new ArgumentNullException("logger"); + } + + Logger = logger; + } + /// /// Processes the message internal. /// @@ -71,7 +91,7 @@ namespace MediaBrowser.Common.Kernel var cancellationTokenSource = new CancellationTokenSource(); - Logger.LogInfo("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name); + Logger.Info("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name); var timer = new Timer(TimerCallback, message.Connection, Timeout.Infinite, Timeout.Infinite); @@ -135,7 +155,7 @@ namespace MediaBrowser.Common.Kernel } catch (Exception ex) { - Logger.LogException("Error sending web socket message {0}", ex, Name); + Logger.ErrorException("Error sending web socket message {0}", ex, Name); DisposeConnection(tuple); } finally @@ -167,7 +187,7 @@ namespace MediaBrowser.Common.Kernel /// The connection. private void DisposeConnection(Tuple connection) { - Logger.LogInfo("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name); + Logger.Info("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name); try { diff --git a/MediaBrowser.Common/Logging/Logger.cs b/MediaBrowser.Common/Logging/Logger.cs deleted file mode 100644 index a6cb6d885..000000000 --- a/MediaBrowser.Common/Logging/Logger.cs +++ /dev/null @@ -1,89 +0,0 @@ -using MediaBrowser.Model.Logging; -using System; - -namespace MediaBrowser.Common.Logging -{ - /// - /// Class Logger - /// - public static class Logger - { - /// - /// Gets or sets the logger instance. - /// - /// The logger instance. - internal static ILogger LoggerInstance { get; set; } - - /// - /// Logs the info. - /// - /// The message. - /// The param list. - public static void LogInfo(string message, params object[] paramList) - { - LogEntry(message, LogSeverity.Info, null, paramList); - } - - /// - /// Logs the debug info. - /// - /// The message. - /// The param list. - public static void LogDebugInfo(string message, params object[] paramList) - { - LogEntry(message, LogSeverity.Debug, null, paramList); - } - - /// - /// Logs the exception. - /// - /// The message. - /// The ex. - /// The param list. - public static void LogException(string message, Exception ex, params object[] paramList) - { - LogEntry(message, LogSeverity.Error, ex, paramList); - } - - /// - /// Logs the warning. - /// - /// The message. - /// The param list. - public static void LogWarning(string message, params object[] paramList) - { - LogEntry(message, LogSeverity.Warn, null, paramList); - } - - /// - /// Logs the entry. - /// - /// The message. - /// The level. - /// The exception. - /// The param list. - private static void LogEntry(string message, LogSeverity level, Exception exception, params object[] paramList) - { - if (LoggerInstance == null) - { - return; - } - - if (exception == null) - { - LoggerInstance.Log(level, message, paramList); - } - else - { - if (level == LogSeverity.Fatal) - { - LoggerInstance.FatalException(message, exception, paramList); - } - else - { - LoggerInstance.ErrorException(message, exception, paramList); - } - } - } - } -} diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index fa4d956db..b80cd8e84 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -187,7 +187,6 @@ - diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs index 3cce146fa..4da5a69f3 100644 --- a/MediaBrowser.Common/Plugins/BasePlugin.cs +++ b/MediaBrowser.Common/Plugins/BasePlugin.cs @@ -189,7 +189,7 @@ namespace MediaBrowser.Common.Plugins get { // Lazy load - LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath) as TConfigurationType); + LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath, Logger) as TConfigurationType); return _configuration; } protected set @@ -274,15 +274,21 @@ namespace MediaBrowser.Common.Plugins /// Starts the plugin. /// /// The kernel. + /// The logger. /// kernel - public void Initialize(IKernel kernel) + public void Initialize(IKernel kernel, ILogger logger) { if (kernel == null) { throw new ArgumentNullException("kernel"); } - Logger = LogManager.GetLogger(Name); + if (logger == null) + { + throw new ArgumentNullException("logger"); + } + + Logger = logger; Kernel = kernel; diff --git a/MediaBrowser.Common/Plugins/IPlugin.cs b/MediaBrowser.Common/Plugins/IPlugin.cs index 2427e6a13..fdd99868a 100644 --- a/MediaBrowser.Common/Plugins/IPlugin.cs +++ b/MediaBrowser.Common/Plugins/IPlugin.cs @@ -107,8 +107,9 @@ namespace MediaBrowser.Common.Plugins /// Starts the plugin. /// /// The kernel. + /// The logger. /// kernel - void Initialize(IKernel kernel); + void Initialize(IKernel kernel, ILogger logger); /// /// Disposes the plugins. Undos all actions performed during Init. diff --git a/MediaBrowser.Common/Serialization/XmlSerializer.cs b/MediaBrowser.Common/Serialization/XmlSerializer.cs index 8737aa818..c1258b72e 100644 --- a/MediaBrowser.Common/Serialization/XmlSerializer.cs +++ b/MediaBrowser.Common/Serialization/XmlSerializer.cs @@ -3,6 +3,7 @@ using System; using System.IO; using System.Linq; using System.Xml; +using MediaBrowser.Model.Logging; namespace MediaBrowser.Common.Serialization { @@ -157,10 +158,11 @@ namespace MediaBrowser.Common.Serialization /// /// The type. /// The path. + /// The logger. /// System.Object. - public static object GetXmlConfiguration(Type type, string path) + public static object GetXmlConfiguration(Type type, string path, ILogger logger) { - Logger.LogInfo("Loading {0} at {1}", type.Name, path); + logger.Info("Loading {0} at {1}", type.Name, path); object configuration; @@ -184,7 +186,7 @@ namespace MediaBrowser.Common.Serialization // If the file didn't exist before, or if something has changed, re-save if (buffer == null || !buffer.SequenceEqual(newBytes)) { - Logger.LogInfo("Saving {0} to {1}", type.Name, path); + logger.Info("Saving {0} to {1}", type.Name, path); // Save it after load in case we got new items File.WriteAllBytes(path, newBytes); @@ -200,11 +202,12 @@ namespace MediaBrowser.Common.Serialization /// /// /// The path. + /// The logger. /// ``0. - public static T GetXmlConfiguration(string path) + public static T GetXmlConfiguration(string path, ILogger logger) where T : class { - return GetXmlConfiguration(typeof(T), path) as T; + return GetXmlConfiguration(typeof(T), path, logger) as T; } } } -- cgit v1.2.3