Packof7.Australia.Logging 3.0.0

Pack of 7 Australia - Logging

There is a newer version of this package available.
See the version list below for details.
Install-Package Packof7.Australia.Logging -Version 3.0.0
dotnet add package Packof7.Australia.Logging --version 3.0.0
<PackageReference Include="Packof7.Australia.Logging" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Packof7.Australia.Logging --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Packof7.Australia.Logging, 3.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Packof7.Australia.Logging as a Cake Addin
#addin nuget:?package=Packof7.Australia.Logging&version=3.0.0

// Install Packof7.Australia.Logging as a Cake Tool
#tool nuget:?package=Packof7.Australia.Logging&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
5.0.1 38 4/8/2021
5.0.0 39 4/8/2021
3.0.1 97 1/14/2021
3.0.0 205 11/14/2020
2.0.0 154 11/4/2020