You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common/Logging/Logger.cs

94 lines
2.8 KiB

13 years ago
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using MediaBrowser.Common.Kernel;
13 years ago
namespace MediaBrowser.Common.Logging
13 years ago
{
public static class Logger
{
internal static IKernel Kernel { get; set; }
13 years ago
public static void LogInfo(string message, params object[] paramList)
{
LogEntry(message, LogSeverity.Info, paramList);
13 years ago
}
public static void LogDebugInfo(string message, params object[] paramList)
{
LogEntry(message, LogSeverity.Debug, paramList);
13 years ago
}
public static void LogError(string message, params object[] paramList)
{
LogEntry(message, LogSeverity.Error, paramList);
13 years ago
}
public static void LogException(Exception ex, params object[] paramList)
{
LogException(string.Empty, ex, paramList);
}
13 years ago
public static void LogException(string message, Exception ex, params object[] paramList)
{
var builder = new StringBuilder();
if (ex != null)
{
builder.AppendFormat("Exception. Type={0} Msg={1} StackTrace={3}{2}",
ex.GetType().FullName,
ex.Message,
ex.StackTrace,
Environment.NewLine);
}
message = FormatMessage(message, paramList);
LogError(string.Format("{0} ( {1} )", message, builder));
13 years ago
}
public static void LogWarning(string message, params object[] paramList)
{
LogEntry(message, LogSeverity.Warning, paramList);
}
private static void LogEntry(string message, LogSeverity severity, params object[] paramList)
{
message = FormatMessage(message, paramList);
Thread currentThread = Thread.CurrentThread;
var row = new LogRow
{
Severity = severity,
Message = message,
ThreadId = currentThread.ManagedThreadId,
ThreadName = currentThread.Name,
Time = DateTime.Now
};
if (Kernel.Loggers != null)
{
foreach (var logger in Kernel.Loggers)
{
logger.LogEntry(row);
}
}
}
private static string FormatMessage(string message, params object[] paramList)
{
if (paramList != null)
{
for (int i = 0; i < paramList.Length; i++)
{
message = message.Replace("{" + i + "}", paramList[i].ToString());
}
}
return message;
13 years ago
}
}
}