TPJ.Logging 10.0.0

dotnet add package TPJ.Logging --version 10.0.0
                    
NuGet\Install-Package TPJ.Logging -Version 10.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="TPJ.Logging" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TPJ.Logging" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TPJ.Logging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TPJ.Logging --version 10.0.0
                    
#r "nuget: TPJ.Logging, 10.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.
#:package TPJ.Logging@10.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TPJ.Logging&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TPJ.Logging&version=10.0.0
                    
Install as a Cake Tool

TPJ.Logging

TPJ.Logging is a simple error logging package for .NET applications. It can log errors to:

  • a text file
  • email
  • or both

The package registers IErrorLogger with dependency injection so it can be used in console apps, ASP.NET Core APIs, and other .NET applications.

Install

dotnet add package TPJ.Logging

Basic configuration

The package reads its settings from configuration.

appsettings.json

This is the simplest setup using file logging:

{
  "TPJ": {
    "Logging": {
      "ApplicationName": "Sample App",
      "Error": {
        "LogType": "LogFile",
        "LogFileDirectory": "C:\\Logs\\TPJ"
      }
    }
  }
}

Available log types

  • Email
  • LogFile
  • EmailLogFile

If you use Email or EmailLogFile, also configure:

  • TPJ:Logging:Error:Email:From
  • TPJ:Logging:Error:Email:To

and the required TPJ.Email settings for sending email.

Console app example

For a console app, the easiest approach is to use Host.CreateApplicationBuilder so configuration and dependency injection are available.

Program.cs

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using TPJ.Logging;

var builder = Host.CreateApplicationBuilder(args);

builder.Configuration["ASPNETCORE_ENVIRONMENT"] ??= builder.Environment.EnvironmentName;

builder.Services.AddTPJLogging();
builder.Services.AddTransient<DemoRunner>();

using var host = builder.Build();

await host.Services.GetRequiredService<DemoRunner>().RunAsync();

internal sealed class DemoRunner(IErrorLogger errorLogger)
{
    public async Task RunAsync()
    {
        try
        {
            throw new InvalidOperationException("Something went wrong in the console app.");
        }
        catch (Exception ex)
        {
            await errorLogger.LogAsync(
                MethodBase.GetCurrentMethod()!,
                ex,
                new { Command = "demo" },
                "Console app example");

            Console.WriteLine("The error was logged.");
        }
    }
}

If your console app uses appsettings.json, make sure the file is copied to the output directory.

ASP.NET Core API example

Register the package during startup, then inject IErrorLogger into a controller or endpoint.

Program.cs

using TPJ.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddTPJLogging();

var app = builder.Build();

app.MapControllers();

app.Run();

Example controller

using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using TPJ.Logging;

namespace SampleApi.Controllers;

[ApiController]
[Route("api/[controller]")]
public class DemoController(IErrorLogger errorLogger) : ControllerBase
{
    [HttpGet("error")]
    public async Task<IActionResult> Error()
    {
        try
        {
            throw new InvalidOperationException("Something went wrong in the API.");
        }
        catch (Exception ex)
        {
            await errorLogger.LogAsync(
                MethodBase.GetCurrentMethod()!,
                ex,
                new
                {
                    Path = Request.Path.Value,
                    TraceIdentifier = HttpContext.TraceIdentifier
                },
                "API example");

            return Problem("The error was logged.");
        }
    }
}

Notes

  • IErrorLogger supports sync and async logging.
  • You can pass additional details as an object or array of objects.
  • MethodBase.GetCurrentMethod() is used so the logger can include method information in the log output.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
10.0.0 67 5/9/2026
9.0.0 266 12/18/2024
4.0.0 483 1/28/2023
3.1.2 1,752 8/19/2017
3.1.1 1,571 8/16/2017
3.0.0 1,350 8/15/2017
2.5.1.1 1,324 8/14/2017
2.5.1 1,375 7/12/2017
2.5.0 1,710 3/21/2017
2.2.3 1,980 10/28/2016
2.1.1 1,988 8/26/2016
Loading failed

V10.0.0 now runs on .NET 10 see github for more details