ExtremelySimpleLogger/Sample/Program.cs

42 lines
1.4 KiB
C#
Raw Normal View History

2020-07-23 02:24:18 +02:00
using System;
2020-07-23 13:45:43 +02:00
using System.Collections.Generic;
2020-07-23 02:24:18 +02:00
using System.IO;
using ExtremelySimpleLogger;
namespace Sample {
internal static class Program {
2020-07-23 02:46:34 +02:00
private static void Main() {
2020-07-23 13:45:43 +02:00
// When we have multiple loggers that output to the same file,
// we can just reuse our sinks
var sinks = new List<Sink> {
// We want to log messages of every log level, so we set the minimum level to Trace
new FileSink("Log.txt", true) {MinimumLevel = LogLevel.Trace},
new ConsoleSink()
};
2020-07-23 02:24:18 +02:00
var logger = new Logger {
2020-07-23 02:58:25 +02:00
Name = "Example Logger",
2020-07-23 13:45:43 +02:00
Sinks = sinks
};
var otherLogger = new Logger {
Name = "Special Logger",
Sinks = sinks
2020-07-23 02:24:18 +02:00
};
logger.Info("Logger loaded.");
2020-07-23 13:45:43 +02:00
// Logging an exception
2020-07-23 02:24:18 +02:00
logger.Warn("Unsafe code follows!");
try {
File.OpenRead("does/not/exist");
} catch (Exception e) {
logger.Error("An exception was thrown!", e);
}
2020-07-23 13:45:43 +02:00
otherLogger.Info("This is a special message from the special logger!");
2020-07-23 02:46:34 +02:00
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.");
2020-07-23 02:24:18 +02:00
}
}
}