mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-30 03:13:17 +01:00
Initial check-in
This commit is contained in:
80
MediaBrowser.Common/Logging/BaseLogger.cs
Normal file
80
MediaBrowser.Common/Logging/BaseLogger.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
public abstract class BaseLogger
|
||||
{
|
||||
public LogSeverity LogSeverity { get; set; }
|
||||
|
||||
public void LogInfo(string message, params object[] paramList)
|
||||
{
|
||||
LogEntry(message, LogSeverity.Info, paramList);
|
||||
}
|
||||
|
||||
public void LogDebugInfo(string message, params object[] paramList)
|
||||
{
|
||||
LogEntry(message, LogSeverity.Debug, paramList);
|
||||
}
|
||||
|
||||
public void LogError(string message, params object[] paramList)
|
||||
{
|
||||
LogEntry(message, LogSeverity.Error, paramList);
|
||||
}
|
||||
|
||||
public void LogException(string message, Exception exception, params object[] paramList)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
if (exception != null)
|
||||
{
|
||||
var trace = new StackTrace(exception, true);
|
||||
builder.AppendFormat("Exception. Type={0} Msg={1} Src={2} Method={5} Line={6} Col={7}{4}StackTrace={4}{3}",
|
||||
exception.GetType().FullName,
|
||||
exception.Message,
|
||||
exception.Source,
|
||||
exception.StackTrace,
|
||||
Environment.NewLine,
|
||||
trace.GetFrame(0).GetMethod().Name,
|
||||
trace.GetFrame(0).GetFileLineNumber(),
|
||||
trace.GetFrame(0).GetFileColumnNumber());
|
||||
}
|
||||
|
||||
StackFrame frame = new StackFrame(1);
|
||||
|
||||
message = string.Format(message, paramList);
|
||||
|
||||
LogError(string.Format("{0} ( {1} )", message, builder));
|
||||
}
|
||||
|
||||
public void LogWarning(string message, params object[] paramList)
|
||||
{
|
||||
LogEntry(message, LogSeverity.Warning, paramList);
|
||||
}
|
||||
|
||||
private void LogEntry(string message, LogSeverity severity, params object[] paramList)
|
||||
{
|
||||
if (severity < LogSeverity) return;
|
||||
|
||||
message = string.Format(message, paramList);
|
||||
|
||||
Thread currentThread = Thread.CurrentThread;
|
||||
|
||||
LogRow row = new LogRow()
|
||||
{
|
||||
Severity = severity,
|
||||
Message = message,
|
||||
Category = string.Empty,
|
||||
ThreadId = currentThread.ManagedThreadId,
|
||||
ThreadName = currentThread.Name,
|
||||
Time = DateTime.Now
|
||||
};
|
||||
|
||||
LogEntry(row);
|
||||
}
|
||||
|
||||
protected abstract void LogEntry(LogRow row);
|
||||
}
|
||||
}
|
||||
55
MediaBrowser.Common/Logging/FileLogger.cs
Normal file
55
MediaBrowser.Common/Logging/FileLogger.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
public class FileLogger : BaseLogger, IDisposable
|
||||
{
|
||||
private string LogDirectory { get; set; }
|
||||
private string CurrentLogFile { get; set; }
|
||||
|
||||
private FileStream FileStream { get; set; }
|
||||
|
||||
public FileLogger(string logDirectory)
|
||||
{
|
||||
LogDirectory = logDirectory;
|
||||
}
|
||||
|
||||
private void EnsureStream()
|
||||
{
|
||||
if (FileStream == null)
|
||||
{
|
||||
if (!Directory.Exists(LogDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(LogDirectory);
|
||||
}
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
CurrentLogFile = Path.Combine(LogDirectory, now.ToString("dMyyyy") + "-" + now.Ticks + ".log");
|
||||
|
||||
FileStream = new FileStream(CurrentLogFile, FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void LogEntry(LogRow row)
|
||||
{
|
||||
EnsureStream();
|
||||
|
||||
byte[] bytes = new UTF8Encoding().GetBytes(row.ToString() + Environment.NewLine);
|
||||
|
||||
FileStream.Write(bytes, 0, bytes.Length);
|
||||
|
||||
FileStream.Flush();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (FileStream != null)
|
||||
{
|
||||
FileStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
118
MediaBrowser.Common/Logging/LogRow.cs
Normal file
118
MediaBrowser.Common/Logging/LogRow.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
public struct LogRow
|
||||
{
|
||||
|
||||
const string TimePattern = "h:mm:ss.fff tt d/M/yyyy";
|
||||
|
||||
|
||||
public LogSeverity Severity { get; set; }
|
||||
public string Message { get; set; }
|
||||
public string Category { get; set; }
|
||||
public int ThreadId { get; set; }
|
||||
public string ThreadName { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string ShortMessage
|
||||
{
|
||||
get
|
||||
{
|
||||
var message = Message;
|
||||
if (message.Length > 120)
|
||||
{
|
||||
message = Message.Substring(0, 120).Replace(Environment.NewLine, " ") + " ... ";
|
||||
}
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
public string FullDescription
|
||||
{
|
||||
get
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendFormat("Time: {0}", Time);
|
||||
sb.AppendLine();
|
||||
sb.AppendFormat("Thread Id: {0} {1}", ThreadId, ThreadName);
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(Message);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append(Time.ToString(TimePattern))
|
||||
.Append(" , ")
|
||||
.Append(Enum.GetName(typeof(LogSeverity), Severity))
|
||||
.Append(" , ")
|
||||
.Append(Encode(Message))
|
||||
.Append(" , ")
|
||||
.Append(Encode(Category))
|
||||
.Append(" , ")
|
||||
.Append(ThreadId)
|
||||
.Append(" , ")
|
||||
.Append(Encode(ThreadName));
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private string Encode(string str)
|
||||
{
|
||||
return (str ?? "").Replace(",", ",,").Replace(Environment.NewLine, " [n] ");
|
||||
}
|
||||
|
||||
public static LogRow FromString(string message)
|
||||
{
|
||||
var split = splitString(message);
|
||||
return new LogRow()
|
||||
{
|
||||
Time = DateTime.ParseExact(split[0], TimePattern, null),
|
||||
Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), split[1]),
|
||||
Message = split[2],
|
||||
Category = split[3],
|
||||
ThreadId = int.Parse(split[4]),
|
||||
ThreadName = split[5]
|
||||
};
|
||||
}
|
||||
|
||||
static string[] splitString(string message)
|
||||
{
|
||||
List<string> items = new List<string>();
|
||||
bool gotComma = false;
|
||||
|
||||
StringBuilder currentItem = new StringBuilder();
|
||||
|
||||
foreach (var chr in message)
|
||||
{
|
||||
|
||||
if (chr == ',' && gotComma)
|
||||
{
|
||||
gotComma = false;
|
||||
currentItem.Append(',');
|
||||
}
|
||||
else if (chr == ',')
|
||||
{
|
||||
gotComma = true;
|
||||
}
|
||||
else if (gotComma)
|
||||
{
|
||||
items.Add(currentItem.ToString().Replace(" [n] ", Environment.NewLine).Trim());
|
||||
currentItem = new StringBuilder();
|
||||
gotComma = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentItem.Append(chr);
|
||||
}
|
||||
|
||||
}
|
||||
items.Add(currentItem.ToString().Replace("[n]", Environment.NewLine).Trim());
|
||||
return items.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
MediaBrowser.Common/Logging/LogSeverity.cs
Normal file
18
MediaBrowser.Common/Logging/LogSeverity.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
[Flags]
|
||||
public enum LogSeverity
|
||||
{
|
||||
None = 0,
|
||||
Debug = 1,
|
||||
Info = 2,
|
||||
Warning = 4,
|
||||
Error = 8
|
||||
}
|
||||
}
|
||||
38
MediaBrowser.Common/Logging/Logger.cs
Normal file
38
MediaBrowser.Common/Logging/Logger.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
public static BaseLogger LoggerInstance { get; set; }
|
||||
|
||||
public static void LogInfo(string message, params object[] paramList)
|
||||
{
|
||||
LoggerInstance.LogInfo(message, paramList);
|
||||
}
|
||||
|
||||
public static void LogDebugInfo(string message, params object[] paramList)
|
||||
{
|
||||
LoggerInstance.LogDebugInfo(message, paramList);
|
||||
}
|
||||
|
||||
public static void LogError(string message, params object[] paramList)
|
||||
{
|
||||
LoggerInstance.LogError(message, paramList);
|
||||
}
|
||||
|
||||
public static void LogException(string message, Exception ex, params object[] paramList)
|
||||
{
|
||||
LoggerInstance.LogException(message, ex, paramList);
|
||||
}
|
||||
|
||||
public static void LogWarning(string message, params object[] paramList)
|
||||
{
|
||||
LoggerInstance.LogWarning(message, paramList);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user