2020-11-18 00:22:24 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
namespace ln.logging
|
|
|
|
|
{
|
|
|
|
|
public class Logger
|
|
|
|
|
{
|
|
|
|
|
public static ConsoleLogger ConsoleLogger { get; } = new ConsoleLogger();
|
|
|
|
|
public static Logger Default { get; set; } = new Logger(ConsoleLogger);
|
|
|
|
|
|
|
|
|
|
public static TextWriter SysOut = Console.Out;
|
|
|
|
|
public static TextWriter SysError = Console.Error;
|
|
|
|
|
|
|
|
|
|
public static void CaptureConsole(Logger logger)
|
|
|
|
|
{
|
|
|
|
|
LoggingWriter outWriter = new LoggingWriter(logger);
|
|
|
|
|
Console.SetOut(outWriter);
|
|
|
|
|
LoggingWriter errorWriter = new LoggingWriter(logger, LogLevel.ERROR);
|
|
|
|
|
Console.SetError(errorWriter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HashSet<ILoggingBackend> Backends { get; } = new HashSet<ILoggingBackend>();
|
|
|
|
|
|
|
|
|
|
public Logger(ILoggingBackend backend)
|
|
|
|
|
{
|
|
|
|
|
Backends.Add(backend);
|
|
|
|
|
}
|
2020-11-27 19:53:07 +01:00
|
|
|
|
public Logger(Stream logStream)
|
|
|
|
|
{
|
|
|
|
|
Backends.Add(new FileLogger(logStream));
|
|
|
|
|
}
|
2020-11-18 00:22:24 +01:00
|
|
|
|
|
|
|
|
|
public void Log(LogLevel level,string message, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
Log(level,String.Format(message, args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(string message,params object[] args){
|
|
|
|
|
Log(LogLevel.INFO,String.Format(message, args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.INFO, message);
|
|
|
|
|
}
|
|
|
|
|
public void Log(LogLevel level,string message)
|
|
|
|
|
{
|
|
|
|
|
string[] lines = message.Split('\n');
|
|
|
|
|
|
|
|
|
|
lines[0] = String.Format("{0} [{1}] [ {3,-10} ] {2}",
|
|
|
|
|
DateTime.Now.ToLocalTime(),
|
|
|
|
|
Process.GetCurrentProcess().Id,
|
|
|
|
|
lines[0],
|
|
|
|
|
level.ToString()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (int n = 1; n < lines.Length; n++){
|
|
|
|
|
lines[n] = " " + lines[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Send(level,lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Send(LogLevel logLevel,String[] lines)
|
|
|
|
|
{
|
|
|
|
|
foreach (ILoggingBackend backend in Backends)
|
|
|
|
|
backend.Message(logLevel,lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Log(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
stringBuilder.AppendFormat("Exception {0} [{1}]\n", exception.GetType().Name,exception.Message);
|
|
|
|
|
|
|
|
|
|
StackTrace stackTrace = new StackTrace(exception, true);
|
|
|
|
|
|
|
|
|
|
foreach (StackFrame stackFrame in stackTrace.GetFrames())
|
|
|
|
|
{
|
|
|
|
|
MethodBase methodBase = stackFrame.GetMethod();
|
|
|
|
|
MethodBody methodBody = methodBase.GetMethodBody();
|
|
|
|
|
|
|
|
|
|
stringBuilder.AppendFormat(" {0} in {1}:{2}\n",
|
|
|
|
|
methodBase.Name,
|
|
|
|
|
stackFrame.GetFileName(),
|
|
|
|
|
stackFrame.GetFileLineNumber()
|
|
|
|
|
);
|
|
|
|
|
//if (methodBody != null)
|
|
|
|
|
//{
|
|
|
|
|
// foreach (LocalVariableInfo lvi in methodBody.LocalVariables)
|
|
|
|
|
// {
|
|
|
|
|
// stringBuilder.AppendFormat(" {0}\n", lvi);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log(LogLevel.ERROR, stringBuilder.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|