Skip to content

Simple logging library solution for fast implementation into any C# project.

Notifications You must be signed in to change notification settings

Abedron/Logging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logging

Simple logging library solution for fast implementation into any C# project.

Introduction

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.

Example

Simple

static void Main(string[] args)
{
    Log.Debug("Hello");
    
    Log.Debug("Hello", "World");
}

Output:

Hello
[World] Hello

With management

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!

Custom Writer Class

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();
        }
    }
}

Custom Writer implementation

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!

About

Simple logging library solution for fast implementation into any C# project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages