AbLibs.AbLogger
1.1.0
dotnet add package AbLibs.AbLogger --version 1.1.0
NuGet\Install-Package AbLibs.AbLogger -Version 1.1.0
<PackageReference Include="AbLibs.AbLogger" Version="1.1.0" />
<PackageVersion Include="AbLibs.AbLogger" Version="1.1.0" />
<PackageReference Include="AbLibs.AbLogger" />
paket add AbLibs.AbLogger --version 1.1.0
#r "nuget: AbLibs.AbLogger, 1.1.0"
#:package AbLibs.AbLogger@1.1.0
#addin nuget:?package=AbLibs.AbLogger&version=1.1.0
#tool nuget:?package=AbLibs.AbLogger&version=1.1.0
AbLogger
A quick and dirty logging library for when you don't need anything more than the ability to write to the console and/or a file in a quick and dirty way.
If consistency checking, thread safety and error handling sounds like things you don't need to care about, then AbLogger is exactly right level for you.
Disclaimer: This is a TOY-LEVEL library and should absolutely, in no way, ever, EVER, be used for anything you might ever put in the same room as a the word "productionized".
Instructions
Import abLogger:
using abLogger;
Create a new log manager:
AbLogger logger = new AbLogger();
So far there are no actual log handlers, so calling the log methods won't do anything. So now add log handlers as required.
Adding Log Handlers
Console logger
AddConsoleLogger(LogLevel level)
Adds a log handler that will write any messages meeting the given threshold level to the console. For example, to add a console logger that only writes WARN and ERROR items:
logger.AddConsoleLogger(LogLevel.WARN);
Note: if a console logger has already been added, this method will instead update the existing one with the given log level.
File logger
AddFileLogger(LogLevel level, string filename, FileInitMode fileMode)
Adds a log handler that will write any messages meeting the given threshold to the given file.
FileInitMode has one of two values:
Replace
: If the file already exists, delete it and start freshAppend
: If the file already exists, leave it in place and add log messages to the end
This method can also be called as:
AddFileLogger(LogLevel level, string filename)
which will default the FileInitMode to Append.
or as :
AddFileLogger(LogLevel level)
which will create a random temporary file and use that.
In all three, the filename is the return value of the method call.
If a FileLogger with the given method exists then it will instead be updated with the given log level.
Writing Logs
The AbLogger class has four methods for writing log entries at different log levels:
Debug(String tag, string text);
Info(string tag, string text);
Warn(string tag, string text);
Error(string tag, string text, Exception? ex);
Where:
tag
is a short string representing a source, event ID, feature, etctext
is the main log messageex
is an exception which can optionally be included with the Error call
When called, these four methods will write the message using all log handlers with an appropriate log level threshold.
Full example
AbLogger logger = new AbLogger();
// Only put important stuff on the console
logger.AddConsoleLogger(LogLevel.WARN);
// Write everything to a temp file (and keep the filename so we know where it went)
string fullLog = logger.AddFileLogger(LogLevel.DEBUG);
// Also write errors to a separate file for reporting
logger.AddFileLogger(LogLevel.DEBUG, "./errors.log", FileInitMode.APPEND);
logger.Debug("Sample", "This message will only go to the big log file");
logger.Warn("Sample", "This item will go to the console and the big log file");
logger.Error("Sample", "This message will only go to all three", new Exception("This exception will be recorded too");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Updated version to streamline the interface a bit