NWrath.Logging
1.0.14
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
<PackageReference Include="NWrath.Logging" Version="1.0.14" />
paket add NWrath.Logging --version 1.0.14
#r "nuget: NWrath.Logging, 1.0.14"
// 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:
Loggers and features
Out the box loggers:
- Console logger
- File logger
- Rolling file logger
- DB(mssql) logger
- Debug(visual studio output) logger
- Lambda logger
- Empty logger
- Composite logger
- 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 | Versions 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. |
-
.NETFramework 4.5.2
- FastExpressionCompiler (>= 1.10.1)
- Newtonsoft.Json (>= 12.0.1)
- NWrath.Synergy (>= 1.0.2)
- System.Data.SqlClient (>= 4.6.0)
- System.Threading.Tasks.Dataflow (>= 4.9.0)
-
.NETStandard 2.0
- FastExpressionCompiler (>= 1.10.1)
- Newtonsoft.Json (>= 12.0.1)
- NWrath.Synergy (>= 1.0.2)
- System.Data.SqlClient (>= 4.6.0)
- System.Threading.Tasks.Dataflow (>= 4.9.0)
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.