LoggerEnhancer.Log 1.2.0

dotnet add package LoggerEnhancer.Log --version 1.2.0
NuGet\Install-Package LoggerEnhancer.Log -Version 1.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LoggerEnhancer.Log" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LoggerEnhancer.Log --version 1.2.0
#r "nuget: LoggerEnhancer.Log, 1.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LoggerEnhancer.Log as a Cake Addin
#addin nuget:?package=LoggerEnhancer.Log&version=1.2.0

// Install LoggerEnhancer.Log as a Cake Tool
#tool nuget:?package=LoggerEnhancer.Log&version=1.2.0

LoggerEnhancer

LoggerEnhancer is an implementation standard ILogger<T> interface to enhance basic logging from ASP.NET Core.

LoggerEnhancer allows you to add any additional information to each log.

The logger does not require changing the logging interface. It uses the standard ILogger<T> interface from Microsoft

Usage

using LoggerEnhancer;

public void ConfigureServices(IServiceCollection services)
{
    // Register your other dependencies
    services.AddLogging();
    services.AddLoggerEnhancer(); // Add LoggerEnhancer service from LoggerEnhancer namespace
}

In this example, contextual information will be recorded from custom middleware

using LoggerEnhancer.Abstractions;

public class LogMiddleware(RequestDelegate next, IContext context)
{
    public async Task Invoke(HttpContext httpContext)
    {
        var pairs = new Dictionary<string, string>();
        pairs.Add("Username", "air2921");
        pairs.Add("uID", 2921.ToString());

        // ContextId. default value is: "None"
        context.ContextId = Guid.NewGuid().ToString();

        // Contextual information
        context.Pairs = pairs;

        // Flag that determines whether it is necessary to register the log recording time
        context.IsDateLog = true;

        // Adding the names of the keys that must be ignored when writing the log
        // pairs.Add("Username", "air2921"); Will be ignored
        context.KeyIgnore = new HashSet<string> { "Username" };

        // Add logging levels that need to be ignored
        context.IgnoreLevels = new HashSet<LogLevel> { LogLevel.Information };

        await next(httpContext);
        return;
    }
}

public static class LogMiddlewareExtensions
{
    public static IApplicationBuilder UseLog(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<LogMiddleware>();
    }
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Add your anothers middlewares
    app.UseLog();
    // Add your anothers middlewares
}

Now you can use the standard ILogger<T> interface to write logs.

public class LoggerController(ILogger<LoggerController> logger) : ControllerBase
{
    [HttpGet]
    public IActionResult TestLog()
    {
        logger.LogWarning("Here is your any log message"); // Using LogWarning because of LogInformation is ignored
        return StatusCode(200, new { message = "OK" });
    }
}

Example log entry:

warn: webapi.Controllers.LoggerController[0]
      <Context Id>a665fd12-d23d-4cbf-bb99-538f89506fd6</Context Id>
      <Logging Date>5/19/2024 4:42:43 AM</Logging Date>
      <uID>2921</uID>
      <Original log>Here is your any log message</Original log>

If you need to exclude contextual information from a specific log, you can use the ILoggerEnhancer<T> interface to pass the parameter:

public class LoggerController(ILoggerEnhancer<LoggerController> logger) : ControllerBase
{
    [HttpGet]
    public IActionResult TestLog()
    {
        logger.LogWarning("Here is your any log message", contextIgnore: true); // Using LogWarning because of LogInformation is ignored
        return StatusCode(200, new { message = "OK" });
    }
}

Example log entry if context ignored:

warn: webapi.Controllers.LoggerController[0]
      Here is your any log message
Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.2.0 91 5/20/2024
1.1.3 78 5/20/2024
1.1.2 126 5/19/2024
1.1.1 78 5/19/2024
1.1.0 89 5/19/2024
1.0.1 94 5/17/2024
1.0.0 89 5/17/2024