From 170154df18fda463de5107b7767f226fe79da79d Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Tue, 26 Feb 2013 15:07:06 -0500 Subject: assembly consolidation --- .../Logging/NLogger.cs | 201 +++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 MediaBrowser.Common.Implementations/Logging/NLogger.cs (limited to 'MediaBrowser.Common.Implementations/Logging/NLogger.cs') diff --git a/MediaBrowser.Common.Implementations/Logging/NLogger.cs b/MediaBrowser.Common.Implementations/Logging/NLogger.cs new file mode 100644 index 0000000000..c87b58f70a --- /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); + } + } +} -- cgit v1.2.3