![]() |
VOOZH | about |
dotnet add package TPJ.Logging --version 10.0.0
NuGet\Install-Package TPJ.Logging -Version 10.0.0
<PackageReference Include="TPJ.Logging" Version="10.0.0" />
<PackageVersion Include="TPJ.Logging" Version="10.0.0" />Directory.Packages.props
<PackageReference Include="TPJ.Logging" />Project file
paket add TPJ.Logging --version 10.0.0
#r "nuget: TPJ.Logging, 10.0.0"
#:package TPJ.Logging@10.0.0
#addin nuget:?package=TPJ.Logging&version=10.0.0Install as a Cake Addin
#tool nuget:?package=TPJ.Logging&version=10.0.0Install as a Cake Tool
TPJ.Logging is a simple error logging package for .NET applications. It can log errors to:
The package registers IErrorLogger with dependency injection so it can be used in console apps, ASP.NET Core APIs, and other .NET applications.
dotnet add package TPJ.Logging
The package reads its settings from configuration.
appsettings.jsonThis is the simplest setup using file logging:
{
"TPJ": {
"Logging": {
"ApplicationName": "Sample App",
"Error": {
"LogType": "LogFile",
"LogFileDirectory": "C:\\Logs\\TPJ"
}
}
}
}
EmailLogFileEmailLogFileIf you use Email or EmailLogFile, also configure:
TPJ:Logging:Error:Email:FromTPJ:Logging:Error:Email:Toand the required TPJ.Email settings for sending email.
For a console app, the easiest approach is to use Host.CreateApplicationBuilder so configuration and dependency injection are available.
Program.csusing 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.
Register the package during startup, then inject IErrorLogger into a controller or endpoint.
Program.csusing TPJ.Logging;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddTPJLogging();
var app = builder.Build();
app.MapControllers();
app.Run();
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.");
}
}
}
IErrorLogger supports sync and async logging.MethodBase.GetCurrentMethod() is used so the logger can include method information in the log output.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
V10.0.0 now runs on .NET 10 see github for more details