Created
December 7, 2016 02:55
-
-
Save lolp1/028d4d90e4688402f20594df56cff3c7 to your computer and use it in GitHub Desktop.
Simple logger example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.IO; | |
using System.Linq; | |
namespace Logs | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
// register logs | |
Log.Add("file", new FileLogger {Path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt"}); | |
Log.Add("console", new ConsoleLogger()); | |
// write to them | |
Log.WriteLine("Hello world"); | |
Log.Info("Hello world"); | |
Log.Debug("Hello world"); | |
Log.Error("Hello world"); | |
Log.Fatal("Hello world"); | |
// write to just one log instance | |
var logger = Log.Get("file"); | |
logger.WriteLine("Hello, world."); | |
} | |
} | |
public interface ILogger | |
{ | |
void WriteLine(string line); | |
} | |
public enum LogLevel | |
{ | |
None, | |
Debug, | |
Information, | |
Warning, | |
Error, | |
Fatal | |
} | |
public class FileLogger : ILogger | |
{ | |
public string Path { get; set; } | |
public void WriteLine(string line) | |
{ | |
using (var logWriter = new StreamWriter(Path, true)) | |
{ | |
logWriter.WriteLine(line); | |
} | |
} | |
} | |
public class ConsoleLogger : ILogger | |
{ | |
public void WriteLine(string line) => Console.WriteLine(line); | |
} | |
public static class Log | |
{ | |
static readonly ConcurrentDictionary<string, ILogger> Loggers; | |
static Log() | |
{ | |
Loggers = new ConcurrentDictionary<string, ILogger>(); | |
FormatLogEntry = DefaultFormatter; | |
} | |
public static Func<LogLevel, string, object[], string> FormatLogEntry { get; set; } | |
public static ILogger Add(string key, ILogger logger) | |
=> Loggers.AddOrUpdate(key, logger, (s, logger1) => logger); | |
public static bool Contains(string key) => Loggers.ContainsKey(key); | |
public static ILogger Remove(string key) | |
{ | |
ILogger logger; | |
Loggers.TryRemove(key, out logger); | |
return logger; | |
} | |
public static ILogger Get(string key) | |
{ | |
ILogger logger; | |
Loggers.TryGetValue(key, out logger); | |
return logger ?? Loggers.Values.FirstOrDefault(); | |
} | |
public static void WriteLine(string format, params object[] args) => WriteEntry(LogLevel.None, format, args); | |
public static void Debug(string format, params object[] args) => WriteEntry(LogLevel.Debug, format, args); | |
public static void Info(string format, params object[] args) | |
=> WriteEntry(LogLevel.Information, format, args); | |
public static void Warn(string format, params object[] args) => WriteEntry(LogLevel.Warning, format, args); | |
public static void Error(string format, params object[] args) => WriteEntry(LogLevel.Error, format, args); | |
public static void Fatal(string format, params object[] args) => WriteEntry(LogLevel.Fatal, format, args); | |
static string DefaultFormatter(LogLevel type, string format, params object[] args) | |
{ | |
if (args == null) | |
{ | |
return $"{(type == LogLevel.None ? "" : $"[{type}]")}[{DateTime.Now}] {format}"; | |
} | |
if (args.Any()) | |
{ | |
format = string.Format(format, args); | |
} | |
return $"{(type == LogLevel.None ? "" : $"[{type}]")}[{DateTime.Now}] {format}"; | |
} | |
static void WriteEntry(LogLevel logLevel, string format, params object[] args) | |
{ | |
var entry = FormatLogEntry(logLevel, format, args); | |
foreach (var logger in Loggers.Values) | |
{ | |
logger.WriteLine(entry); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment