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); } } }