Utils.DotNetCore.ILogger.Extensions 1.0.0

dotnet add package Utils.DotNetCore.ILogger.Extensions --version 1.0.0                
NuGet\Install-Package Utils.DotNetCore.ILogger.Extensions -Version 1.0.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="Utils.DotNetCore.ILogger.Extensions" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Utils.DotNetCore.ILogger.Extensions --version 1.0.0                
#r "nuget: Utils.DotNetCore.ILogger.Extensions, 1.0.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 Utils.DotNetCore.ILogger.Extensions as a Cake Addin
#addin nuget:?package=Utils.DotNetCore.ILogger.Extensions&version=1.0.0

// Install Utils.DotNetCore.ILogger.Extensions as a Cake Tool
#tool nuget:?package=Utils.DotNetCore.ILogger.Extensions&version=1.0.0                

About

Just a simple extension to use with a standard ILogger from Microsoft. <br> Purpose is to add methods to write log messages with context. Caller class name, caller method name, line number.

How to

Install package with NuGet package manager or with dotnet add package command <br> A few methods can be used:

  • LogWithContext - can write a log message with a LogLevel provided parameter.
  • LogExceptionWithContext - will write down an exception. <br> A caller class name, method name and line number will be added to log output: <br>

Code sample:

public class LogController : Controller
{
    private readonly ILogger<LogController> _logger;

    public LogController(ILogger<LogController> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    [HttpPost]
    public IActionResult InformationalWithContext()
    {
        _logger.LogWithContext(LogLevel.Information, "Test informational message using context");
        return Ok();
    }

    [HttpPost]
    public IActionResult ExceptionWithContext()
    {
        var divider = 0;

        try
        {
            var result = 10 / divider;
        }
        catch (Exception ex)
        {
            _logger.LogExceptionWithContext("Test exception message using context", ex);
        }

        return Ok();
    }
}

Log output sample:

[INF] Test informational message using context (at LogController.InformationalWithContext, line 60)
Test exception message using context (at LogController.ExceptionWithContext, line 75)
System.DivideByZeroException: Attempted to divide by zero. at SerilogExtendedWebExample.Controllers.LogController.ExceptionWithContext()
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Utils.DotNetCore.ILogger.Extensions:

Package Downloads
Utils.DotNetCore.Serilog.Extended

Serilog Extension to help use it in projects. A couple additional extension methods added, sample configuration, self debug, log file name, etc.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 106 10/17/2024

Initial release