From d2aa2e1b327b89a0d2801151a2a7202f472ac379 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 23 Jul 2020 02:24:18 +0200 Subject: [PATCH] Initial commit --- .gitignore | 6 +++ ExtremelySimpleLogger.sln | 16 ++++++++ ExtremelySimpleLogger/ConsoleSink.cs | 11 +++++ .../ExtremelySimpleLogger.csproj | 7 ++++ ExtremelySimpleLogger/FileSink.cs | 30 ++++++++++++++ ExtremelySimpleLogger/LogLevel.cs | 12 ++++++ ExtremelySimpleLogger/Logger.cs | 7 ++++ ExtremelySimpleLogger/Sink.cs | 40 +++++++++++++++++++ Sample/Program.cs | 30 ++++++++++++++ Sample/Sample.csproj | 12 ++++++ 10 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 ExtremelySimpleLogger.sln create mode 100644 ExtremelySimpleLogger/ConsoleSink.cs create mode 100644 ExtremelySimpleLogger/ExtremelySimpleLogger.csproj create mode 100644 ExtremelySimpleLogger/FileSink.cs create mode 100644 ExtremelySimpleLogger/LogLevel.cs create mode 100644 ExtremelySimpleLogger/Logger.cs create mode 100644 ExtremelySimpleLogger/Sink.cs create mode 100644 Sample/Program.cs create mode 100644 Sample/Sample.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91953df --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +.idea \ No newline at end of file diff --git a/ExtremelySimpleLogger.sln b/ExtremelySimpleLogger.sln new file mode 100644 index 0000000..f8bd5da --- /dev/null +++ b/ExtremelySimpleLogger.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtremelySimpleLogger", "ExtremelySimpleLogger\ExtremelySimpleLogger.csproj", "{D323F21E-8DE7-46DB-8E34-E4739876F6CB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D323F21E-8DE7-46DB-8E34-E4739876F6CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D323F21E-8DE7-46DB-8E34-E4739876F6CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D323F21E-8DE7-46DB-8E34-E4739876F6CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D323F21E-8DE7-46DB-8E34-E4739876F6CB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ExtremelySimpleLogger/ConsoleSink.cs b/ExtremelySimpleLogger/ConsoleSink.cs new file mode 100644 index 0000000..fbc246e --- /dev/null +++ b/ExtremelySimpleLogger/ConsoleSink.cs @@ -0,0 +1,11 @@ +using System; + +namespace ExtremelySimpleLogger { + public class ConsoleSink : Sink { + + public override void Log(string s) { + Console.WriteLine(s); + } + + } +} \ No newline at end of file diff --git a/ExtremelySimpleLogger/ExtremelySimpleLogger.csproj b/ExtremelySimpleLogger/ExtremelySimpleLogger.csproj new file mode 100644 index 0000000..2756020 --- /dev/null +++ b/ExtremelySimpleLogger/ExtremelySimpleLogger.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs new file mode 100644 index 0000000..6620b7c --- /dev/null +++ b/ExtremelySimpleLogger/FileSink.cs @@ -0,0 +1,30 @@ +using System; +using System.IO; + +namespace ExtremelySimpleLogger { + public class FileSink : Sink { + + private readonly StreamWriter writer; + + public FileSink(string file, bool append) : + this(new FileInfo(file), append) { + } + + public FileSink(FileInfo file, bool append) { + var dir = file.Directory; + if (dir != null && !dir.Exists) + dir.Create(); + + if (!append && file.Exists) + file.Delete(); + + this.writer = file.AppendText(); + this.writer.AutoFlush = true; + } + + public override void Log(string s) { + this.writer.WriteLine(s); + } + + } +} \ No newline at end of file diff --git a/ExtremelySimpleLogger/LogLevel.cs b/ExtremelySimpleLogger/LogLevel.cs new file mode 100644 index 0000000..d93f54d --- /dev/null +++ b/ExtremelySimpleLogger/LogLevel.cs @@ -0,0 +1,12 @@ +namespace ExtremelySimpleLogger { + public enum LogLevel { + + Trace, + Debug, + Info, + Warn, + Error, + Fatal + + } +} \ No newline at end of file diff --git a/ExtremelySimpleLogger/Logger.cs b/ExtremelySimpleLogger/Logger.cs new file mode 100644 index 0000000..0015371 --- /dev/null +++ b/ExtremelySimpleLogger/Logger.cs @@ -0,0 +1,7 @@ +namespace ExtremelySimpleLogger { + public class Logger { + + + + } +} \ No newline at end of file diff --git a/ExtremelySimpleLogger/Sink.cs b/ExtremelySimpleLogger/Sink.cs new file mode 100644 index 0000000..4571298 --- /dev/null +++ b/ExtremelySimpleLogger/Sink.cs @@ -0,0 +1,40 @@ +using System; +using System.Text; + +namespace ExtremelySimpleLogger { + public abstract class Sink { + + public LogLevel MinimumLevel { get; set; } = LogLevel.Info; + public LogFormatter Formatter { get; set; } + + public Sink() { + this.Formatter = this.FormatDefault; + } + + public void Log(Logger logger, LogLevel level, object message, Exception e = null) { + this.Log(this.Formatter.Invoke(logger, level, message, e)); + } + + public abstract void Log(string s); + + public virtual string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) { + var builder = new StringBuilder(); + // date + builder.Append($"[{DateTime.Now}] "); + // logger name + if (!string.IsNullOrEmpty(logger.Name)) + builder.Append($"[{logger.Name}] "); + // log level + builder.Append($"[{level}] "); + // message + builder.Append(message); + // stack trace + if (e != null) + builder.Append($"\n{e.StackTrace}"); + return builder.ToString(); + } + + public delegate string LogFormatter(Logger logger, LogLevel level, object message, Exception e = null); + + } +} \ No newline at end of file diff --git a/Sample/Program.cs b/Sample/Program.cs new file mode 100644 index 0000000..81c64af --- /dev/null +++ b/Sample/Program.cs @@ -0,0 +1,30 @@ +using System; +using System.IO; +using ExtremelySimpleLogger; + +namespace Sample { + internal static class Program { + + private static void Main(string[] args) { + var logger = new Logger { + Name = "Test Logger", + Sinks = { + new FileSink("Log.txt", true) {MinimumLevel = LogLevel.Trace}, + new ConsoleSink() + } + }; + logger.Info("Logger loaded."); + logger.Info("Program starting."); + + logger.Warn("Unsafe code follows!"); + try { + File.OpenRead("does/not/exist"); + } catch (Exception e) { + logger.Error("An exception was thrown!", e); + } + + logger.Log(LogLevel.Trace, "The program finished."); + } + + } +} \ No newline at end of file diff --git a/Sample/Sample.csproj b/Sample/Sample.csproj new file mode 100644 index 0000000..484cb3f --- /dev/null +++ b/Sample/Sample.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + +