Skip to content

πŸ“ Simple, yet effective, logging with Metadata CallerInfo for creating beautiful Log files in C#/VB .NET

License

Notifications You must be signed in to change notification settings

mrousavy/MetaLog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

76 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Icon

MetaLog

Simple, yet effective, logging with Metadata CallerInfo for creating beautiful Log files in C#/VB .NET

.NET Standard 2.0 Class Library

AppVeyor build status NuGet package downloads

Install via NuGet Package Manager:

PM> Install-Package MetaLog

Usage

Namespace

using MetaLog;

Using the ILogger

// 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!

Using Streams

// 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!

Async

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!

The Exception Tree

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

Using the static Logger class

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!

Performance

.NET Unit-Test Results

License

Thanks for using MetaLog! License: MIT, mrousavy 2017

Buy Me a Coffee at ko-fi.com

About

πŸ“ Simple, yet effective, logging with Metadata CallerInfo for creating beautiful Log files in C#/VB .NET

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages