mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-12-26 14:59:22 +01:00
Initial commit
This commit is contained in:
commit
d2aa2e1b32
10 changed files with 171 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
bin/
|
||||
obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
.idea
|
16
ExtremelySimpleLogger.sln
Normal file
16
ExtremelySimpleLogger.sln
Normal file
|
@ -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
|
11
ExtremelySimpleLogger/ConsoleSink.cs
Normal file
11
ExtremelySimpleLogger/ConsoleSink.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace ExtremelySimpleLogger {
|
||||
public class ConsoleSink : Sink {
|
||||
|
||||
public override void Log(string s) {
|
||||
Console.WriteLine(s);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
7
ExtremelySimpleLogger/ExtremelySimpleLogger.csproj
Normal file
7
ExtremelySimpleLogger/ExtremelySimpleLogger.csproj
Normal file
|
@ -0,0 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
30
ExtremelySimpleLogger/FileSink.cs
Normal file
30
ExtremelySimpleLogger/FileSink.cs
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
12
ExtremelySimpleLogger/LogLevel.cs
Normal file
12
ExtremelySimpleLogger/LogLevel.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace ExtremelySimpleLogger {
|
||||
public enum LogLevel {
|
||||
|
||||
Trace,
|
||||
Debug,
|
||||
Info,
|
||||
Warn,
|
||||
Error,
|
||||
Fatal
|
||||
|
||||
}
|
||||
}
|
7
ExtremelySimpleLogger/Logger.cs
Normal file
7
ExtremelySimpleLogger/Logger.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace ExtremelySimpleLogger {
|
||||
public class Logger {
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
40
ExtremelySimpleLogger/Sink.cs
Normal file
40
ExtremelySimpleLogger/Sink.cs
Normal file
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
30
Sample/Program.cs
Normal file
30
Sample/Program.cs
Normal file
|
@ -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.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
12
Sample/Sample.csproj
Normal file
12
Sample/Sample.csproj
Normal file
|
@ -0,0 +1,12 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ExtremelySimpleLogger\ExtremelySimpleLogger.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in a new issue