diff --git a/ExtremelySimpleLogger/ConsoleSink.cs b/ExtremelySimpleLogger/ConsoleSink.cs index fbc246e..4b9753f 100644 --- a/ExtremelySimpleLogger/ConsoleSink.cs +++ b/ExtremelySimpleLogger/ConsoleSink.cs @@ -1,8 +1,15 @@ using System; namespace ExtremelySimpleLogger { + /// + /// A that writes log output to . + /// public class ConsoleSink : Sink { + /// + /// Logs the given message, which has already been formatted using . + /// + /// The message to log public override void Log(string s) { Console.WriteLine(s); } diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs index 6620b7c..39f3b27 100644 --- a/ExtremelySimpleLogger/FileSink.cs +++ b/ExtremelySimpleLogger/FileSink.cs @@ -1,15 +1,27 @@ -using System; using System.IO; namespace ExtremelySimpleLogger { + /// + /// A that writes log output to a file. + /// public class FileSink : Sink { private readonly StreamWriter writer; + /// + /// Creates a new file sink with the given settings. + /// + /// The full, or relative, path of the file to write to + /// Whether new output should be appended to the old log file public FileSink(string file, bool append) : this(new FileInfo(file), append) { } + /// + /// Creates a new file sink with the given settings. + /// + /// The full, or relative, path of the file to write to + /// Whether new output should be appended to the old log file public FileSink(FileInfo file, bool append) { var dir = file.Directory; if (dir != null && !dir.Exists) @@ -22,6 +34,10 @@ namespace ExtremelySimpleLogger { this.writer.AutoFlush = true; } + /// + /// Logs the given message, which has already been formatted using . + /// + /// The message to log public override void Log(string s) { this.writer.WriteLine(s); } diff --git a/ExtremelySimpleLogger/LogLevel.cs b/ExtremelySimpleLogger/LogLevel.cs index d93f54d..8776fb8 100644 --- a/ExtremelySimpleLogger/LogLevel.cs +++ b/ExtremelySimpleLogger/LogLevel.cs @@ -1,11 +1,33 @@ namespace ExtremelySimpleLogger { + /// + /// A log level represents the importance of a message. + /// The higher the log level (the farther down in the list), the more important it is. + /// public enum LogLevel { + /// + /// The log level for very high-detail messages that are used for intensive debugging + /// Trace, + /// + /// The log level for high-detail messages that are used for debugging + /// Debug, + /// + /// The log level for informational messages + /// Info, + /// + /// The log level for warnings. + /// Warn, + /// + /// The log level for errors. + /// Error, + /// + /// The log level for fatal exceptions, like when the program encounters a crash. + /// Fatal } diff --git a/ExtremelySimpleLogger/Logger.cs b/ExtremelySimpleLogger/Logger.cs index 607704b..d93e8b1 100644 --- a/ExtremelySimpleLogger/Logger.cs +++ b/ExtremelySimpleLogger/Logger.cs @@ -2,17 +2,47 @@ using System; using System.Collections.Generic; namespace ExtremelySimpleLogger { + /// + /// The implementation of a logger, which is a wrapper class around multiple logging . + /// + /// To start logging with a logger, its need to be initialized. + /// There are two default implementations: and . + /// public class Logger { + /// + /// All of the instances that this logger logs to. + /// By default, and are available. + /// public List Sinks { get; set; } = new List(); + /// + /// The minimum that a message needs to have for this logger to log it. + /// public LogLevel MinimumLevel { get; set; } = LogLevel.Trace; + /// + /// If this property is set to false, this logger will not log any messages. + /// public bool IsEnabled { get; set; } = true; + /// + /// The name of this logger. This name is used in by default. + /// public string Name { get; set; } + /// + /// Initializes a new logger with the given name. + /// Note that, for this logger to do anything, its need to be initialized. + /// + /// The logger's name public Logger(string name = "") { this.Name = name; } + /// + /// Logs a message, passing it on to this logger's . + /// + /// The importance level of this message + /// The message + /// An optional exception whose stack trace will be appended to the message public void Log(LogLevel level, object message, Exception e = null) { if (!this.IsEnabled || level < this.MinimumLevel) return; @@ -22,11 +52,43 @@ namespace ExtremelySimpleLogger { } } + /// + /// Logs a message with the log level. + /// + /// The message public void Trace(object message) => this.Log(LogLevel.Trace, message); + + /// + /// Logs a message with the log level. + /// + /// The message public void Debug(object message) => this.Log(LogLevel.Debug, message); + + /// + /// Logs a message with the log level. + /// + /// The message public void Info(object message) => this.Log(LogLevel.Info, message); + + /// + /// Logs a message with the log level. + /// + /// The message + /// An optional exception whose stack trace will be appended to the message public void Warn(object message, Exception e = null) => this.Log(LogLevel.Warn, message, e); + + /// + /// Logs a message with the log level. + /// + /// The message + /// An optional exception whose stack trace will be appended to the message public void Error(object message, Exception e = null) => this.Log(LogLevel.Error, message, e); + + /// + /// Logs a message with the log level. + /// + /// The message + /// An optional exception whose stack trace will be appended to the message public void Fatal(object message, Exception e = null) => this.Log(LogLevel.Fatal, message, e); } diff --git a/ExtremelySimpleLogger/Sink.cs b/ExtremelySimpleLogger/Sink.cs index 4571298..1cdea42 100644 --- a/ExtremelySimpleLogger/Sink.cs +++ b/ExtremelySimpleLogger/Sink.cs @@ -2,21 +2,59 @@ using System; using System.Text; namespace ExtremelySimpleLogger { + /// + /// A sink is a way for log messages passed to a to be processed in a certain way. + /// By default, and are available. + /// public abstract class Sink { + /// + /// The minimum level that a log message needs to have for it to be processed by this sink. + /// public LogLevel MinimumLevel { get; set; } = LogLevel.Info; + /// + /// The with which this message should be formatted. + /// By default, is used. + /// public LogFormatter Formatter { get; set; } + /// + /// Initializes a new sink with the default settings. + /// public Sink() { this.Formatter = this.FormatDefault; } - public void Log(Logger logger, LogLevel level, object message, Exception e = null) { + /// + /// Logs a message in the way specified by the sink's implementation. + /// + /// The logger that the message was passed to + /// The importance level of this message + /// The message + /// An optional exception whose stack trace will be appended to the message + public virtual void Log(Logger logger, LogLevel level, object message, Exception e = null) { this.Log(this.Formatter.Invoke(logger, level, message, e)); } + /// + /// Logs the given message, which has already been formatted using . + /// + /// The message to log public abstract void Log(string s); + /// + /// The default formatter for logging messages. + /// By default, messages are laid out as follows: + /// + /// [Date and time] [Logger name, if set] [Log level] Message + /// Exception, if set + /// + /// + /// The logger that the message was passed to + /// The importance level of this message + /// The message + /// An optional exception whose stack trace will be appended to the message + /// A formatted string to log public virtual string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) { var builder = new StringBuilder(); // date @@ -34,6 +72,13 @@ namespace ExtremelySimpleLogger { return builder.ToString(); } + /// + /// A delegate method used by . + /// + /// The logger that the message was passed to + /// The importance level of this message + /// The message + /// An optional exception whose stack trace will be appended to the message public delegate string LogFormatter(Logger logger, LogLevel level, object message, Exception e = null); } diff --git a/Sample/Program.cs b/Sample/Program.cs index 81c64af..6a63bc0 100644 --- a/Sample/Program.cs +++ b/Sample/Program.cs @@ -5,11 +5,11 @@ using ExtremelySimpleLogger; namespace Sample { internal static class Program { - private static void Main(string[] args) { + private static void Main() { var logger = new Logger { Name = "Test Logger", Sinks = { - new FileSink("Log.txt", true) {MinimumLevel = LogLevel.Trace}, + new FileSink("Log.txt", true) {MinimumLevel = LogLevel.Trace}, new ConsoleSink() } }; @@ -23,7 +23,8 @@ namespace Sample { logger.Error("An exception was thrown!", e); } - logger.Log(LogLevel.Trace, "The program finished."); + logger.Log(LogLevel.Trace, "This is a message that only the file sink will receive, since its minimum level is lower."); + logger.Log(LogLevel.Info, "The program finished."); } }