ProphetsWay.Logger
3.0.1-420.Beta
Prefix Reserved
dotnet add package ProphetsWay.Logger --version 3.0.1-420.Beta
NuGet\Install-Package ProphetsWay.Logger -Version 3.0.1-420.Beta
<PackageReference Include="ProphetsWay.Logger" Version="3.0.1-420.Beta" />
paket add ProphetsWay.Logger --version 3.0.1-420.Beta
#r "nuget: ProphetsWay.Logger, 3.0.1-420.Beta"
// Install ProphetsWay.Logger as a Cake Addin #addin nuget:?package=ProphetsWay.Logger&version=3.0.1-420.Beta&prerelease // Install ProphetsWay.Logger as a Cake Tool #tool nuget:?package=ProphetsWay.Logger&version=3.0.1-420.Beta&prerelease
ProphetsWay.Logger
Master Build Status | NuGet Alpha | NuGet Beta | NuGet Release |
---|---|---|---|
Logger is a quick to setup logging utility. It is designed so that you can establish the destinations for your log messages to one or many different outputs, each targeting different log level severities (so you can have a log of Errors and Warnings separate from a logger dumping Debug and Info statements).
Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Prerequisites
You can pull a copy of the source code from GitHub, or you can reference the library from NuGet.org from within Visual Studio.
Install-Package ProphetsWay.Logger
dotnet add package ProphetsWay.Logger
Referencing and Using
Because this is the first utility of a handful, they will all exist in the namespace of "ProphetsWay.Utilities". Since the Logger is a static object, you can use it throughout your software to log messages without having to worry about any pre-configuration. However the point is to be able to create LoggerDestinations where ever you want, with a minimum specified LogLevel severity. You should establish your LoggerDestinations when your project initializes, so that the destinations are only created once.
using ProphetsWay.Utilities;
To create a simple Console Logging Destination, just instantiate a new "ConsoleDestination" class, and add it to the logger.
Logger.AddDestination(new ConsoleDestination());
or
var dest = new ConsoleDestination();
Logger.AddDestination(dest);
Destinations can be added and removed so long as you keep a reference to the object from when you initially added it. There is also the option to "Clear" all current destinations and simply add new ones.
Logger.RemoveDestination(dest);
and
Logger.ClearDestinations();
The built in destinations consist of ConsoleDestination, FileDestination, and EventDestination. There is also a base abstract class BaseLoggingDestination that anyone can use to inherit and implement their own custom destination.
ConsoleDestination will simply dump the log statement to your console, for both the console and file destinations, there is a text formatter that will append a timestamp and the log level to the message before it is rendered to the console.
Hello World!
is transformed into
3/15/2019 11:09:33 PM :: Debug: Hello World!
with the log level adding padding, so that scrolling thru the text will align timestamps, levels, and first characters of your message. Exceptions are also dumped into your logs as well.
3/15/2019 11:25:41 PM :: Error: Another generic message about an error occuring. (friendly message to show a UI maybe?)
This exception has an inner exception. (likely details to hide from a UI)
Inner Exception Message:
This is a specific Exception Message and will contain a stack trace.
A FileDestination requires a target filename to dump the logs into. If you want to clear the file on launch of your application (so that the file only contains logs from the 'last run') you can leave the reset flag as true, or if you want to have a running log, you can set it to false.
var fileDest = new FileDestination("Warnings.log", LogLevels.Warning, false);
A final additional parameter is available to specify the text Encoder to be used. The following are supported
- ASCII
- BigEndianUnicode
- Unicode
- UTF8
- UTF32
UTF8 is set by default.
The last destination available by default is the EventDestination. With this you will create the destination and then assign a delegate to the EventHandler. As a valid statement is logged, the event will trigger, and you can handle the message however you please. Generally this is the destination I use in my UI's. I will use Dispatcher to invoke the UI to render the log messages into a UI control for the user to see.
var evtDest = new EventDestination(LogLevels.Debug);
evtDest.LoggingEvent += (sender, eventArgs) => { /* whatever you want to do with the message here */ };
For simple actions in specific situations, the EventDestination is likely your best option. However if you have some specific functionality that you will use in a few different projects specific to your situation, you can also create your own personalized destinations. An example of a custom destination might be to create a specific database destination that is tightly coupled to a solution you are working in across multiple projects.
The BaseLoggingDestination requires an argument of LogLevel to be established, so any log statement with a severity of what was chosen or higher will be logged at that destination. The log levels are as follows:
Debug - All LogLevels will log to this level
Information - Everything but Debug messages will log to this level
Security - Error, Warning, and Security messages will log to this level
Warning - Only Warnings and Errors
Error - Only Errors will log to this level
//added in 2.0 new LogLevels
DebugOnly - Will only accept Debug level messages
InformationOnly - Will only accept Information level messages
SecurityOnly - Will only accept Security level messages
WarningOnly - Will only accept Warning level messages
If your curious why there isn't an "ErrorOnly" option, that's because Error already works that way.
If you set your destination to LogLevels.Debug, then all messages currently supported will be logged in your destination. If you choose LogLevels.Warning, then only Warning and Error logs will be logged in your destination.
You can have as many destinations as you wish; if you want to create two log files, one for Warnings and Errors, and a second one for all your Debug/Info logs, you only have to create two separate FileDestinations and add them both to the Logger.
var debugDest = new FileDestination("Debug.log", LogLevels.Debug);
var warnDest = new FileDestination("Warnings.log", LogLevels.Warning);
Logger.AddDestination(debugDest);
Logger.AddDestination(warnDest);
Using Generic LoggingDestinations
Now you can create a generic ILoggingDestination<T>
and pass an object to be logged along
with your message and/or exception. Currently included is new destination GenericEventDestination<T>
,
but it is inferred that if you want to include additional metadata to the log statements then you
will likely need to create a custom destination to handle the additional information in your metadata object.
Custom Destination Setup
public class DbMetadata{
public int UserId { get; }
public string EntryPointMethod { get; }
public object Permissions { get; }
//etc whatever other properties you would want
}
public class MyCustomDbDestination : BaseLoggingDestination<DbMetadata>{
public MyCustomDbDestination(LogLevels reportingLevel) : base(reportingLevel) { }
//setup the connection to the database for example
private context _db;
public override void WriteLogEntry(T metadata, LogLevels level, string message = null, Exception thrownException = null)
{
//for any given log message, write all the details passed to the database record
_db.WriteLogRecord(message, level, metadata.UserId, metadata.EntryPointMethod, metadata.Permissions);
}
}
//example program usage
public void Main(string[] args){
Logger.AddDestination(new MyCustomDbDestination(LogLevels.Debug));
var meta = new DbMetadata{...};
//...
Logger.Debug("I've got a debug statement to write...'", meta);
}
Usage of Custom Destination
public void main(string[] args){
var dest = new MyCustomDbDestination(LogLevels.Debug);
Logger.AddDestination(dest);
var context = new DbMetadata();
//do stuff here
//update properties of the context object
Logger.Debug("Logging a message that something happened.", context);
try{
//do something in here
}
catch(Exception ex){
Logger.Error(ex, context);
}
}
For more examples of how Generic logger can work, feel free to check out the Test project.
Metadata Extensions
If you inherit the interface ILoggerMetadata
then you will have access to all the logging functionality
right on your metadata object. With the above example, simply modify DbMetadata
as follows:
public class DbMetadata : ILoggingMetadata{
public int UserId { get; }
public string EntryPointMethod { get; }
public object Permissions { get; }
//etc whatever other properties you would want
}
As you can see, no actual methods or properties are added to your object when you inherit the new interface. However adding it will allow you to log statements right off your object.
//example program usage
public void Main(string[] args){
Logger.AddDestination(new MyCustomDbDestination(LogLevels.Debug));
var meta = new DbMetadata{...};
//...
meta.Debug("I've got a debug statement to write...");
}
Running the unit tests
The library is up to 48 unit tests currently. I tried to cover everything possible. They are created with XUnit and utilize Moq for two tests. The Test project is included in this repository, as well as an Example project.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- G. Gordon Nasseri - Initial work - ProphetManX
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details
P.S.
With the release of 2.0, all of the features I've wanted to add have been put into the project, as of now there are no new features coming. Any updates should be bug fixes found when using the project, or if the community likes this project and submits feature requests, I will definitely try to work them in.
Cheers!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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. 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. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 is compatible. netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net40 is compatible. net403 was computed. net45 is compatible. net451 is compatible. net452 is compatible. net46 is compatible. net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 is compatible. net472 was computed. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.1
- No dependencies.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETFramework 4.5.1
- No dependencies.
-
.NETFramework 4.5.2
- No dependencies.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETFramework 4.7.1
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.6
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net5.0
- No dependencies.
-
net6.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.
Version | Downloads | Last updated |
---|---|---|
3.0.1-420.Beta | 124 | 5/13/2022 |
3.0.1-420.Alpha | 107 | 5/13/2022 |
3.0.1-418.Alpha | 112 | 5/13/2022 |
3.0.1-352.Beta | 104 | 4/27/2022 |
3.0.1-352.Alpha | 99 | 4/27/2022 |
3.0.0 | 471 | 1/26/2021 |
3.0.0-248.Beta | 142 | 1/26/2021 |
3.0.0-248.Alpha | 135 | 1/26/2021 |
2.2.0-238.Alpha | 161 | 1/26/2021 |
2.1.0 | 529 | 6/3/2020 |
2.1.0-182.Beta | 232 | 6/3/2020 |
2.1.0-182.Alpha | 223 | 6/3/2020 |
2.0.0 | 727 | 4/20/2019 |
2.0.0-105.Alpha | 281 | 4/21/2019 |
2.0.0-97.Beta | 279 | 4/20/2019 |
2.0.0-97.Alpha | 287 | 4/20/2019 |
1.3.0 | 682 | 4/20/2019 |
1.3.0-95.Beta | 285 | 4/20/2019 |
1.3.0-95.Alpha | 275 | 4/20/2019 |
1.3.0-94.Alpha | 290 | 4/20/2019 |
1.3.0-93.Alpha | 285 | 4/20/2019 |
1.2.0 | 800 | 4/6/2019 |
1.2.0-Beta-89 | 450 | 4/6/2019 |
1.2.0-Beta-88 | 424 | 4/6/2019 |
1.2.0-Alpha-89 | 460 | 4/6/2019 |
1.2.0-Alpha-88 | 417 | 4/4/2019 |
1.2.0-Alpha-87 | 435 | 4/4/2019 |
1.1.0 | 589 | 3/24/2019 |
1.1.0-Beta-85 | 436 | 3/24/2019 |
1.1.0-Alpha-85 | 437 | 3/24/2019 |
1.1.0-Alpha-84 | 426 | 3/24/2019 |
1.1.0-Alpha-81 | 441 | 3/24/2019 |
1.1.0-Alpha-79 | 442 | 3/24/2019 |
1.1.0-Alpha-75 | 422 | 3/24/2019 |
1.1.0-Alpha-74 | 452 | 3/24/2019 |
1.1.0-Alpha-72 | 449 | 3/24/2019 |
1.0.0 | 593 | 3/16/2019 |
1.0.0-Alpha-51 | 438 | 3/16/2019 |
1.0.0-Alpha-50 | 430 | 3/16/2019 |
1.0.0-Alpha-49 | 433 | 3/16/2019 |
0.0.0-Alpha-48 | 452 | 3/16/2019 |
0.0.0-Alpha-46 | 463 | 3/16/2019 |
0.0.0-Alpha-43 | 451 | 3/7/2019 |
0.0.0-Alpha-42 | 463 | 3/6/2019 |
0.0.0-Alpha-41 | 461 | 3/6/2019 |
0.0.0-Alpha-36 | 455 | 3/4/2019 |
0.0.0-Alpha-34 | 422 | 3/4/2019 |
0.0.0-Alpha-32 | 466 | 3/2/2019 |
0.0.0-Alpha-30 | 454 | 3/2/2019 |
0.0.0-Alpha-28 | 464 | 3/2/2019 |
# v3.0.1
### Build target for Net 6.0
Library now targets .Net 6.0
Updated repository to build using the new https://github.com/ProphetManX/prophets-pipelines framework