Initial Commit

master
Harald Wolff-Thobaben 2020-11-18 00:22:24 +01:00
commit 573e50b867
11 changed files with 481 additions and 0 deletions

41
.gitignore vendored 100644
View File

@ -0,0 +1,41 @@
# Autosave files
*~
# build
[Oo]bj/
[Bb]in/
packages/
TestResults/
# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
.vs/
# Mac bundle stuff
*.dmg
*.app
# resharper
*_Resharper.*
*.Resharper
# dotCover
*.dotCover

17
ConsoleLogger.cs 100644
View File

@ -0,0 +1,17 @@
using System;
namespace ln.logging
{
public class ConsoleLogger : LoggingBackend
{
public ConsoleLogger()
{
}
protected override void MessageReceived(LogLevel logLevel, string[] lines)
{
foreach (string line in lines){
Logger.SysOut.WriteLine(line);
}
}
}
}

73
FileLogger.cs 100644
View File

@ -0,0 +1,73 @@
using System;
using System.IO;
namespace ln.logging
{
public class FileLogger : LoggingBackend, IDisposable
{
FileStream fileStream;
TextWriter writer;
public FileLogger(string filename)
{
openLogFile(filename);
}
private void openLogFile(string filename)
{
if (File.Exists(filename))
{
String oldfn = String.Format("{0}.old", filename);
if (File.Exists(oldfn))
File.Delete(oldfn);
File.Move(filename, oldfn);
}
fileStream = new FileStream(filename, FileMode.Create);
writer = new StreamWriter(fileStream);
}
public void Close()
{
if (writer != null)
{
writer.Close();
fileStream.Close();
}
}
public void Dispose()
{
if (writer != null)
{
writer.Close();
writer.Dispose();
writer = null;
}
if (fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
fileStream = null;
}
}
protected override void MessageReceived(LogLevel logLevel, string[] lines)
{
if (writer == null)
{
throw new ObjectDisposedException("FileLogger has already been disposed");
}
lock (writer)
{
foreach (String line in lines)
{
writer.WriteLine(line);
writer.Flush();
}
}
}
}
}

12
ILoggingBackend.cs 100644
View File

@ -0,0 +1,12 @@
using System;
using System.Security.Cryptography.X509Certificates;
namespace ln.logging
{
public interface ILoggingBackend
{
LogLevel MinLogLevel { get; set; }
LogLevel MaxLogLevel { get; set; }
void Message(LogLevel logLevel,String[] lines);
}
}

15
LogLevel.cs 100644
View File

@ -0,0 +1,15 @@
using System;
namespace ln.logging
{
public enum LogLevel
{
FATAL = 0,
ERROR = 5,
WARNING = 10,
INFO = 15,
DEBUG = 30,
DEBUGDETAIL = 40,
DEBUGFULL = 80,
MAX = 128
}
}

109
Logger.cs 100644
View File

@ -0,0 +1,109 @@
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);
}
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());
}
}
}

19
Logging.cs 100644
View File

@ -0,0 +1,19 @@
using System;
namespace ln.logging
{
public static class Logging
{
public static void Log(string message, params object[] args)
{
Log(LogLevel.INFO, message, args);
}
public static void Log(LogLevel level,string message, params object[] args)
{
Logger.Default.Log(level,message, args);
}
public static void Log(Exception e){
Log(LogLevel.ERROR,"Exception: {0} / {1}\n{2}", e.GetType().Name, e.Message, e.StackTrace.ToString());
}
}
}

28
LoggingBackend.cs 100644
View File

@ -0,0 +1,28 @@
using System;
namespace ln.logging
{
public abstract class LoggingBackend : ILoggingBackend
{
public LogLevel MinLogLevel { get; set; } = LogLevel.FATAL;
public LogLevel MaxLogLevel { get; set; } = LogLevel.MAX;
public LoggingBackend()
{
}
public void Message(LogLevel logLevel, string[] lines)
{
if ((logLevel >= MinLogLevel) && (logLevel <= MaxLogLevel))
MessageReceived(logLevel, lines);
}
protected abstract void MessageReceived(LogLevel logLevel, String[] lines);
public void Log(LogLevel logLevel,String message,params object[] p)
{
string[] lines = String.Format(message, p).Split('\n', '\r');
Message(logLevel, lines);
}
}
}

47
LoggingWriter.cs 100644
View File

@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace ln.logging
{
class LoggingWriter : TextWriter
{
Logger logger;
LogLevel level;
List<char> linebuffer = new List<char>();
public LoggingWriter(Logger logger, LogLevel level = LogLevel.INFO)
{
this.logger = logger;
this.level = level;
}
public override void Write(char value)
{
if (value == '\n')
{
logger.Log(level, new String(linebuffer.ToArray()));
linebuffer.Clear();
}
else
{
linebuffer.Add(value);
}
}
public override void WriteLine(string format, params object[] args)
{
logger.Log(level, format, args);
}
public override Encoding Encoding
{
get
{
return Encoding.UTF8;
}
}
}
}

110
MemoryLogger.cs 100644
View File

@ -0,0 +1,110 @@
// /**
// * File: MemoryLogger.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using ln.logging;
using System.Linq;
namespace ln.logging
{
public class MemoryLogger : ILoggingBackend
{
private LogEntry[] Entries = new LogEntry[256];
private int CurrentLogPointer = 255;
public MemoryLogger()
{
}
public MemoryLogger(int bufferLength)
{
Entries = new LogEntry[bufferLength];
CurrentLogPointer = Entries.Length - 1;
}
public LogLevel MinLogLevel { get; set; } = LogLevel.FATAL;
public LogLevel MaxLogLevel { get; set; } = LogLevel.INFO;
public void Message(LogLevel logLevel, string[] lines)
{
lock (this)
{
Entries[CurrentLogPointer].LogLevel = logLevel;
Entries[CurrentLogPointer].Lines = String.Join("\n", lines);
CurrentLogPointer = NextLogPointer;
}
}
public LogEntry[] GetLogEntries()
{
lock (this)
{
LogEntry[] prepare = new LogEntry[Entries.Length];
if (CurrentLogPointer == (Entries.Length - 1))
{
Array.Copy(Entries, prepare, Entries.Length);
}
else
{
Array.Copy(Entries, PreviousLogPointer, prepare, 0, Entries.Length - PreviousLogPointer);
Array.Copy(Entries, 0, prepare, Entries.Length - PreviousLogPointer, PreviousLogPointer);
}
return prepare.Where((x) => x.Lines != null).ToArray();
}
}
public LogEntry[] GetLogEntries(LogLevel maxLogLevel)
{
lock (this)
{
LogEntry[] prepare = new LogEntry[Entries.Length];
if (CurrentLogPointer == (Entries.Length - 1))
{
Array.Copy(Entries, prepare, Entries.Length);
}
else
{
Array.Copy(Entries, PreviousLogPointer, prepare, 0, Entries.Length - PreviousLogPointer);
Array.Copy(Entries, 0, prepare, Entries.Length - PreviousLogPointer, PreviousLogPointer);
}
return prepare.Where((x) => (x.Lines != null) && (maxLogLevel >= x.LogLevel)).ToArray();
}
}
private int NextLogPointer
{
get
{
int current = CurrentLogPointer;
current--;
if (current < 0)
current = Entries.Length - 1;
return current;
}
}
private int PreviousLogPointer
{
get
{
int current = CurrentLogPointer;
current++;
if (current >= Entries.Length)
current = 0;
return current;
}
}
public struct LogEntry
{
public LogLevel LogLevel;
public string Lines;
}
}
}

10
ln.logging.csproj 100644
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>
</Project>