From 170154df18fda463de5107b7767f226fe79da79d Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Tue, 26 Feb 2013 15:07:06 -0500 Subject: assembly consolidation --- .../Logging/LogHelper.cs | 90 +++++++++ .../Logging/NLogger.cs | 201 +++++++++++++++++++++ .../Logging/NlogManager.cs | 144 +++++++++++++++ .../MediaBrowser.Common.Implementations.csproj | 6 + .../packages.config | 1 + 5 files changed, 442 insertions(+) create mode 100644 MediaBrowser.Common.Implementations/Logging/LogHelper.cs create mode 100644 MediaBrowser.Common.Implementations/Logging/NLogger.cs create mode 100644 MediaBrowser.Common.Implementations/Logging/NlogManager.cs (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/Logging/LogHelper.cs b/MediaBrowser.Common.Implementations/Logging/LogHelper.cs new file mode 100644 index 000000000..4afd302d5 --- /dev/null +++ b/MediaBrowser.Common.Implementations/Logging/LogHelper.cs @@ -0,0 +1,90 @@ +using System; +using System.Text; + +namespace MediaBrowser.Common.Implementations.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.Implementations/Logging/NLogger.cs b/MediaBrowser.Common.Implementations/Logging/NLogger.cs new file mode 100644 index 000000000..c87b58f70 --- /dev/null +++ b/MediaBrowser.Common.Implementations/Logging/NLogger.cs @@ -0,0 +1,201 @@ +using MediaBrowser.Model.Logging; +using System; +using System.Text; + +namespace MediaBrowser.Common.Implementations.Logging +{ + /// + /// Class NLogger + /// + public 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.Implementations/Logging/NlogManager.cs b/MediaBrowser.Common.Implementations/Logging/NlogManager.cs new file mode 100644 index 000000000..1e525137c --- /dev/null +++ b/MediaBrowser.Common.Implementations/Logging/NlogManager.cs @@ -0,0 +1,144 @@ +using MediaBrowser.Model.Logging; +using NLog; +using NLog.Config; +using NLog.Targets; +using System; +using System.IO; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Implementations.Logging +{ + /// + /// Class NlogManager + /// + public class NlogManager : ILogManager + { + /// + /// Occurs when [logger loaded]. + /// + public event EventHandler LoggerLoaded; + /// + /// Gets or sets the log directory. + /// + /// The log directory. + private string LogDirectory { get; set; } + /// + /// Gets or sets the log file prefix. + /// + /// The log file prefix. + private string LogFilePrefix { get; set; } + /// + /// Gets the log file path. + /// + /// The log file path. + public string LogFilePath { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The log directory. + /// The log file name prefix. + public NlogManager(string logDirectory, string logFileNamePrefix) + { + LogDirectory = logDirectory; + LogFilePrefix = logFileNamePrefix; + } + + /// + /// Adds the file target. + /// + /// The path. + /// The level. + private void AddFileTarget(string path, LogSeverity level) + { + var logFile = new FileTarget(); + + logFile.FileName = path; + logFile.Layout = "${longdate}, ${level}, ${logger}, ${message}"; + + AddLogTarget(logFile, "ApplicationLogFile", level); + } + + /// + /// Adds the log target. + /// + /// The target. + /// The name. + /// The level. + private void AddLogTarget(Target target, string name, LogSeverity level) + { + var config = LogManager.Configuration; + + config.RemoveTarget(name); + + target.Name = name; + config.AddTarget(name, target); + + var rule = new LoggingRule("*", GetLogLevel(level), target); + config.LoggingRules.Add(rule); + + LogManager.Configuration = config; + } + + /// + /// Gets the logger. + /// + /// The name. + /// ILogger. + public ILogger GetLogger(string name) + { + return new NLogger(name); + } + + /// + /// Gets the log level. + /// + /// The severity. + /// LogLevel. + /// Unrecognized LogSeverity + private LogLevel GetLogLevel(LogSeverity severity) + { + switch (severity) + { + case LogSeverity.Debug: + return LogLevel.Debug; + case LogSeverity.Error: + return LogLevel.Error; + case LogSeverity.Fatal: + return LogLevel.Fatal; + case LogSeverity.Info: + return LogLevel.Info; + case LogSeverity.Warn: + return LogLevel.Warn; + default: + throw new ArgumentException("Unrecognized LogSeverity"); + } + } + + /// + /// Reloads the logger. + /// + /// The level. + public void ReloadLogger(LogSeverity level) + { + LogFilePath = Path.Combine(LogDirectory, LogFilePrefix + "-" + DateTime.Now.Ticks + ".log"); + + AddFileTarget(LogFilePath, level); + + if (LoggerLoaded != null) + { + Task.Run(() => + { + try + { + LoggerLoaded(this, EventArgs.Empty); + } + catch (Exception ex) + { + GetLogger("Logger").ErrorException("Error in LoggerLoaded event", ex); + } + }); + } + } + } +} diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj index 59ec01aaf..52be41ddd 100644 --- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj +++ b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj @@ -35,6 +35,9 @@ Always + + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll + ..\packages\protobuf-net.2.0.0.621\lib\net40\protobuf-net.dll @@ -56,6 +59,9 @@ + + + diff --git a/MediaBrowser.Common.Implementations/packages.config b/MediaBrowser.Common.Implementations/packages.config index 63fd0052f..2578e48f3 100644 --- a/MediaBrowser.Common.Implementations/packages.config +++ b/MediaBrowser.Common.Implementations/packages.config @@ -1,5 +1,6 @@  + -- cgit v1.2.3