Simple logging library solution for fast implementation into any C# project.
Log
class is singleton without namespace and does not require initialization. You can make custom Writer
for variable targeting (console, database, file, ...). Library contains default writer for console.
static void Main(string[] args)
{
Log.Debug("Hello");
Log.Debug("Hello", "World");
}
Output:
Hello
[World] Hello
static void Main(string[] args)
{
ILogger logger = Log.Logger;
logger.Enabled = false;
Log.Debug("Hide", LogChannel.Init);
logger.Enabled = true;
Log.Debug("Show", LogChannel.Init);
logger.RemoveAllWriters();
Log.Debug("Hide", LogChannel.Init);
logger.AddWriter(logger.DefaultWriter);
Log.Debug("Show: I am here again!", LogChannel.Network);
}
enum LogChannel
{
None,
Init,
Network,
Detector
}
Output:
[Init] Show
[Network] Show: I am here again!
using System.IO;
using System.Text;
using System.Timers;
namespace Logging.Writers
{
public class FileWriter : Writer
{
private StringBuilder stringBuilder;
private Timer timer;
private bool recorded;
private string path;
public FileWriter(LogLevel logLevel) : base(logLevel)
{
path = Directory.GetCurrentDirectory() + "/log.txt";
stringBuilder = new StringBuilder(100);
timer = new Timer(1000);
timer.Start();
timer.Elapsed += TimerHandler;
}
public override void Debug<S>(S message)
{
stringBuilder.Append("Debug: " + message + "\n");
recorded = true;
}
public override void Warning<S>(S message)
{
stringBuilder.Append("Warning: " + message + "\n");
recorded = true;
}
public override void Error<S>(S message)
{
stringBuilder.Append("Error: " + message + "\n");
recorded = true;
}
private void TimerHandler(object sender, ElapsedEventArgs e)
{
if (recorded)
{
File.AppendAllText(path, stringBuilder.ToString());
stringBuilder.Clear();
recorded = false;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
stringBuilder.Clear();
timer.Dispose();
}
}
}
class Program
{
static void Main(string[] args)
{
FileWriter fileWriter = new FileWriter(LogLevel.Debug | LogLevel.Error | LogLevel.Warning);
Log.Logger.AddWriter(fileWriter);
Log.Debug("Hello World!");
Log.Warning("Hello Mars!");
Log.Error("Hello Europe!");
while (true)
{
string message = Console.ReadLine();
Log.Debug(message, "Info");
}
}
}
Output log.txt:
Debug: Hello World!
Warning: Hello Mars!
Error: Hello Europe!