Packof7.Australia.Logging
3.0.1
See the version list below for details.
dotnet add package Packof7.Australia.Logging --version 3.0.1
NuGet\Install-Package Packof7.Australia.Logging -Version 3.0.1
<PackageReference Include="Packof7.Australia.Logging" Version="3.0.1" />
paket add Packof7.Australia.Logging --version 3.0.1
#r "nuget: Packof7.Australia.Logging, 3.0.1"
// Install Packof7.Australia.Logging as a Cake Addin #addin nuget:?package=Packof7.Australia.Logging&version=3.0.1 // Install Packof7.Australia.Logging as a Cake Tool #tool nuget:?package=Packof7.Australia.Logging&version=3.0.1
Packof7.Australia.Logging
This package provides two types of loggers - A colour logger, and a file logger. For setting up each of the loggers, please refer to the documentation below.
Colour Logger
The colour logger is an extension of Microsoft's ILogger, providing log output's to be written in a specified colour depending on the configuration provided. The logger also output's each log in a particular format:
[Date/Time] {Class}
{Log Level}: Provided logging output
Clearing Providers (Optional)
In your Program.cs file under the CreateHostBuilder() method, you can specifiy the following to your webBuilder:
webBuilder.ConfigureLogging(config => {
config.ClearProviders();
}).UseStartup<Startup>();
This will clear the default logging providers in the .NET Core application.
Configuring Startup
To add the Colour Logger as the default logging provider, you will need to edit the parameters of the Configure() method. Take note of the ILoggerFactory, and the ILogger. The ILogger parameter is optional, but is also useful for logging the startup of the application.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, ILogger<Startup> logger)
This will provide the ILoggerFactory provided by Microsoft. We will then need to configure this logger by added custom logging configuration settings. To do this, simply call the following method as an extension on the ILoggerFactory, then provide the Configuration for the specified configuration setting. Ensure that the Packof7.Australia.Logging namespace is provided.
loggerFactory.AddCustomLogger(new LoggerConfiguration
{
LogLevel = LogLevel.Debug,
Color = ConsoleColor.Cyan
});
Example
In the following example, I will use a function to configure the ILoggerFactory for all the necessary logging levels.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, ILogger<Startup> logger)
{
// Default ASP.NET Core setup configuration above...
ConfigureLogging(loggerFactory);
logger.LogInformation("Server has started");
}
/// <summary>
/// Configure Microsoft Logging to use the custom logging
/// settings configured in the following method.
/// </summary>
/// <param name="loggerFactory">ILoggerFactory Interface</param>
private void ConfigureLogger(ILoggerFactory loggerFactory)
{
loggerFactory.AddCustomLogger(new LoggerConfiguration
{
LogLevel = LogLevel.Debug,
Color = ConsoleColor.Cyan
});
loggerFactory.AddCustomLogger(new LoggerConfiguration
{
LogLevel = LogLevel.Information,
Color = ConsoleColor.Green
});
loggerFactory.AddCustomLogger(c =>
{
c.LogLevel = LogLevel.Warning;
c.Color = ConsoleColor.Yellow;
});
loggerFactory.AddCustomLogger(new LoggerConfiguration
{
LogLevel = LogLevel.Error,
Color = ConsoleColor.Red
});
loggerFactory.AddCustomLogger(new LoggerConfiguration
{
LogLevel = LogLevel.Critical,
Color = ConsoleColor.DarkMagenta
});
}
File Logger
The FileLogger is a simple logging tool to log directly to a File/Directory given by given Configuration settings. If the namespace can not be recognised at any point during this configuration process, the FileLogger is provided under Packof7.Australia.Logging.File.
Initialise Configuration Settings
To initialise the configuration settings for the LogFileConfiguration class, you can simply start by adding the configuration settings to your appsettings.json file, or by instantiating a new instance manually
appsettings.json
In your appsettings.json file, add a configuration variable with the necessary information:
Note: The FileName setting can be configured to use {DATE}, which will replace {DATE} with the current DateTime of the creation of the FileLogger. The FileName setting can be configured to use {DATE_UNIQUE} which will replace {DATE_UNIQUE} with a unique number representation of the current time also.
"FileLoggerName": {
"FilePath": "c:\\temp\\",
"FileName": "Test_{DATE_UNIQUE}.log",
"MinLogLevel": "Information",
"Expiry": 2
}
In the Startup.cs file, and then inside of the ConfigureServices() Method, add the following:
services.Configure<LogFileConfiguration>(Configuration.GetSection("FileLoggerName"));
If you wish to use the Configuration Settings inside of another class, then you can use the following:
public ClassA(IOptions<LogFileConfiguration> configuraion)
{
_configuration = configuration.Value;
}
public void SomeMethodInsideClassA()
{
FileLogger fileLogger = new FileLogger(_configuration);
}
Manual Instantiation
To Create a new instance of the configuration settings, you can also do the following:
public void SomeMethod()
{
// Create the LogFileConfiguration variable, and configure the appropriate settings
LogFileConfiguration configuration = new LogFileConfiguration();
configuration.FilePath = "C:\\temp\\";
configuration.FileName = "{DATE_UNIQUE}_file.log";
configuration.Expiry = 0;
configuration.MinLogLevel = "Information";
// Create the FileLogger
FileLogger fileLogger = new FileLogger(configuration)
}
Creating Multiple FileLogger Configurations
Creating multiple configurations is easy and can be done simply by using a wrapper Configuration setting. The following example shows how to create a configuration for multiple file loggers.
Example
Create a new Class for your configuration
public class FileLoggerConfiguration
{
public LogFileConfiguration FileLoggerOne { get; set; }
public LogFileConfiguration FileLoggerTwo { get; set; }
public LogFileConfiguration FileLoggerThree { get; set; }
}
In your appsettings.json, the following can be configured
"FileLoggers": {
"FileLoggerOne": {
"FilePath": "c:\\temp\\",
"FileName": "Test123.log",
"MinLogLevel": "Information",
"Expiry": 2
},
"FileLoggerTwo": {
"FilePath": "c:\\temp\\",
"FileName": "Test456.log",
"MinLogLevel": "Debug",
"Expiry": 30
},
"FileLoggerThree": {
"FilePath": "c:\\temp\\",
"FileName": "Test789.log",
"MinLogLevel": "Error",
"Expiry": 0
}
},
Then finally in your Statup.cs ConfigureServices method
services.Configure<FileLoggerConfiguration>(Configuration.GetSection("FileLoggers"));
You can then inject IOptions<FileLoggerConfiguration> into the necessary services to use each configuration setting
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. 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 | 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 | 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging (>= 2.0.0)
- Packof7.Australia.Extensions (>= 3.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.