Simple, yet effective, logging with Metadata CallerInfo for creating beautiful Log files in C#/VB .NET
.NET Standard 2.0 Class Library
Install via NuGet Package Manager:
PM> Install-Package MetaLog
using MetaLog;
// create a new Logger with log file at Documents\\log.log, and minimum log severity "Info"
ILogger logger = Logger.New("C:\\Users\\mrousavy\\Documents\\log.log", LogSeverity.Info);
logger.Log(LogSeverity.Info, "Logged in as mrousavy, version 1.0!");
// or: logger.Info(..);
/// Output (log.log):
/// [Info] [2017-09-01 20:37:02.755] [StaticTests.CustomTest:16]: Logged in as mrousavy, version 1.0!
// create a new Logger with stream to log file at Documents\\log.log, and minimum log severity "Info"
using(ILogger logger = Logger.New("C:\\Users\\mrousavy\\Documents\\log.log", LogSeverity.Info, true)) {
logger.Log(LogSeverity.Info, "Logged in as mrousavy, version 1.0!");
} //will call Dispose() here, you can also call it manually without the using(..){..} directive
/// Output (log.log):
/// [Info] [2017-09-01 20:37:02.755] [StaticTests.CustomTest:16]: Logged in as mrousavy, version 1.0!
await logger.LogAsync(LogSeverity.Info, "Logged in as mrousavy, version 1.0!");
/// Output (log.log):
/// [Info] [2017-09-01 20:37:02.755] [StaticTests.CustomTest:16]: Logged in as mrousavy, version 1.0!
try {
// something that will throw
} catch (Exception ex) {
logger.Log(LogSeverity.Error, ex);
}
/// Output (log.log):
/// [Error] [2017-09-01 20:53:27.936] [StaticTests.CustomTest:20]: BEGIN EXCEPTION TREE:
/// β System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\mrousavy\test.txt'.
/// β β¬ at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
/// β β at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, /// SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
/// β β at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
/// β β at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
/// β β at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
/// β β at System.IO.File.ReadAllText(String path)
/// β β at MetaLog.Tests.StaticTests.CustomTest() in D:\Projects\MetaLog\MetaLog.Tests\StaticTests.cs:line 18
Logger.LogFile = "C:\\Users\\mrousavy\\Documents\\log.log";
Logger.UseStream = true; //create a new Stream to the File, set to false to dispose the Stream
Logger.Log(LogSeverity.Info, "Logged in as mrousavy, version 1.0!");
/// Output (log.log):
/// [Info] [2017-09-01 20:37:02.755] [StaticTests.CustomTest:16]: Logged in as mrousavy, version 1.0!