NWrath.Logging 1.0.14

There is a newer version of this package available.
See the version list below for details.
dotnet add package NWrath.Logging --version 1.0.14                
NuGet\Install-Package NWrath.Logging -Version 1.0.14                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NWrath.Logging" Version="1.0.14" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NWrath.Logging --version 1.0.14                
#r "nuget: NWrath.Logging, 1.0.14"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NWrath.Logging as a Cake Addin
#addin nuget:?package=NWrath.Logging&version=1.0.14

// Install NWrath.Logging as a Cake Tool
#tool nuget:?package=NWrath.Logging&version=1.0.14                

NWrath.Logging

Simple .NET logging infrastructure.

Getting started

Set up logger by Wizard:

var logger = LoggingWizard.Spell.ConsoleLogger();

Or just create class instance:

var logger = new ConsoleLogger();

Then use it:

logger.Debug("DEBUG MESSAGE");
logger.Info("INFO MESSAGE");
logger.Warning("WARNING MESSAGE");
logger.Error("ERROR MESSAGE", new NotImplementedException("err"));
logger.Critical("CRITICAL MESSAGE", new ApplicationException("err"));
Output:

Console logger output

Loggers and features

Out the box loggers:
  1. Console logger
  2. File logger
  3. Rolling file logger
  4. DB(mssql) logger
  5. Debug(visual studio output) logger
  6. Lambda logger
  7. Empty logger
  8. Composite logger
  9. Background logger
Customizable output template and formats:

Log message model look like:

  • Timestamp: DateTime
  • Message : String
  • Level: LogLevel
  • Exception: Exception
  • Extra: StringSet

For most loggers used string log serializer, and any part of log message can have own format. Default output template for string log serializer: "{Timestamp} [{Level}] {Message}{ExNewLine}{Exception}", and default timestamp format: "yyyy-MM-dd HH:mm:ss.fff" But we can easily change it and even extend.

var logger = LoggingWizard.Spell.ConsoleLogger(s =>
{
    s.OutputTemplate = "{Timestamp} | {machine} | {Message}";
    s.Formats.Timestamp = m => $"{m.Timestamp:HH:mm:ss}";
    s.Formats["machine"] = m => Environment.MachineName;
});
logger.Debug("DEBUG MESSAGE");
Output:
"01:09:21 | MyPC | DEBUG MESSAGE"

Or we can replace default serializer by class that implement IStringLogSerializer.

var logger = LoggingWizard.Spell.ConsoleLogger(
    new JsonLogSerializer()
);
logger.Debug("DEBUG MESSAGE");
Output:
"{"Timestamp":"2018-08-25T01:15:09.8698551+03:00","Message":"DEBUG MESSAGE","Exception":null,"Level":0,"Extra":{}}"

Log level verification

Log level verifier determine can we write log message or not. The are five log levels: Debug, Info, Warning, Error, Critical and three type of implemented verifiers: minimum level verifier, range level verifier, multiple level verifier. By default used minimum log level verifier. Code below not write debug message in output.

var logger = LoggingWizard.Spell.ConsoleLogger(LogLevel.Info)
logger.Debug("DEBUG MESSAGE");
logger.Info("INFO MESSAGE");
logger.Warning("WARNING MESSAGE");
Output:
"2018-08-25 19:33:57.280 [Info] INFO MESSAGE"
"2018-08-25 19:33:57.310 [Warning] WARNING MESSAGE"

Also log level verifier can be a custom class that implement ILogLevelVerifier.

Loggers composition

We can combine loggers by using composite or lambda logger. Use composite logger for write to multiple loggers at once:

//use existed loggers
var logger = LoggingWizard.Spell.CompositeLogger(
                new ConsoleLogger(),
                new DebugLogger()
                );
//use factory
var logger = LoggingWizard.Spell.CompositeLogger(
                f => f.ConsoleLogger(),
                f => f.DebugLogger()
                );

Lambda logger is used for combine loggers or using specific predicates etc.:

//use debug logger for ApplicationException and console logger for other messages
var debugLogger = LoggingWizard.Spell.DebugLogger();
var consoleLogger = LoggingWizard.Spell.ConsoleLogger();

var logger = LoggingWizard.Spell.LambdaLogger(m => {
    if (m.Exception is ApplicationException)
    {
        debugLogger.Log(m);
    }
    else
    {
        consoleLogger.Log(m);
    }
});

BackgroundLogger is a wrapper for another loggers and used for write log in new thread. It contains log records queue, that flush to base logger on queue batch is full or the timer fire. All of this allow write log very fast, queue just make bundle and then all bundle writes by logger at once in new task. Especially that true for loggers that support bundle log, like FileLogger or DbLogger.

//set up file logger as background by Wizard or BackgroundLogger class name
var logger = LoggingWizard.Spell.BackgroundLogger(
                 s => s.FileLogger("log.txt")
                 );
//now we can use it like regular logger
logger.Info("INFO MESSAGE");
//don`t forget dispose when logger no need more
logger.Dispose();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NWrath.Logging:

Package Downloads
NWrath.Logging.AspNetCore

NWrath.Logging integration for ASP.NET Core 2+

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.20 716 3/17/2020
1.0.19 710 12/17/2019
1.0.18 488 12/13/2019
1.0.16 1,410 7/14/2019
1.0.15 555 5/31/2019
1.0.14 765 4/1/2019
1.0.13 567 2/25/2019