Chd.Library.Logging
8.7.2
dotnet add package Chd.Library.Logging --version 8.7.2
NuGet\Install-Package Chd.Library.Logging -Version 8.7.2
<PackageReference Include="Chd.Library.Logging" Version="8.7.2" />
<PackageVersion Include="Chd.Library.Logging" Version="8.7.2" />
<PackageReference Include="Chd.Library.Logging" />
paket add Chd.Library.Logging --version 8.7.2
#r "nuget: Chd.Library.Logging, 8.7.2"
#:package Chd.Library.Logging@8.7.2
#addin nuget:?package=Chd.Library.Logging&version=8.7.2
#tool nuget:?package=Chd.Library.Logging&version=8.7.2
Logging Library for .NET 8
Chd (Cleverly Handle Difficulty) library helps you cleverly handle difficulty, write code quickly, and keep your application stable.
The NuGet package id is Chd.Library.Logging. This package targets .NET 8 (net8.0).
If you are targeting .NET 10, use Chd.Logging instead.
Table of Contents
- About
- Features
- Installing
- Getting Started
- Configuration
- Usage
- Supported Sinks
- Test Results
- Examples & Tests
- Authors
- Acknowledgements
About
Chd.Library.Logging enables aspect-oriented and manual logging for .NET 8 applications.
It can write to Graylog, Microsoft SQL Server, a file, and the console. [Logged] is applied automatically at build time. No extra setup is needed. Log entries include the method name, parameters, return value, and, when an HTTP context is available, user details.
Features
- Aspect logging via the
[Logged]attribute (supports async methods) - Manual logging via the static
Loggerclass (Library.Logging.Utils) - Sinks: Graylog or Microsoft SQL Server (selected with
LogConfig:Sink); file and console writers are also configured - Log entries include method, class, parameter and return-value information
- .NET 8 (
net8.0) - Includes user and request information when
HttpContextis present on the instance
Installing
dotnet add package Chd.Library.Logging --version 8.7.2
NuGet Package Manager:
Install-Package Chd.Library.Logging -Version 8.7.2
Package Reference (.csproj):
<PackageReference Include="Chd.Library.Logging" Version="8.7.2" />
That is the only package you need to add. Logging is applied automatically at build time. No extra setup is needed.
If the project already has a FodyWeavers.xml file, add <MethodBoundaryAspect /> to it so [Logged] runs with your other weavers:
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<MethodBoundaryAspect />
</Weavers>
Upgrading from earlier versions
Earlier versions of this package asked you to install MethodBoundaryAspect.Fody (and Fody) yourself. That setup keeps working: you can upgrade this package and leave those references and a FodyWeavers.xml file in place.
You may optionally remove the manual Fody and MethodBoundaryAspect.Fody package references; weaving is included in this package. If you keep a FodyWeavers.xml file, it must contain <MethodBoundaryAspect />.
Getting Started
1. Enable the logger in your API pipeline
In Program.cs:
var builder = WebApplication.CreateBuilder(args);
// ... register other services
var app = builder.Build();
app.UseLogger();
Call app.UseLogger() at startup, before methods marked with [Logged] run. There is also IServiceCollection.AddLogger(), which performs the same initialization.
Configuration
Logging config in appsettings.json
{
"LogConfig": {
"ApplicationName": "YourApp",
"Sink": "Graylog",
"SinkDBConnectionString": "Server=.;Database=LogsDb;User Id=sa;Password=YourPass;",
"SinkTableName": "Logs",
"Server": "localhost",
"Port": "3003",
"Path": "C:\\Logs\\YourApp\\log.txt",
"PathFormat": "C:\\Logs\\YourApp\\log-{Date}.txt",
"FileSizeLimitBytes": 1073741824,
"RetainedFileCountLimit": 10,
"IsLogSessionInfo": false
}
}
| Key | Used when | Description |
|---|---|---|
Sink |
Always | "Graylog" (default) or "MSSql". Any other value is treated as Graylog. |
Server, Port |
Graylog | Graylog host and UDP port. |
SinkDBConnectionString |
Sink is MSSql |
SQL Server connection string. If omitted, ConnectionStrings:LogsDb or ConnectionStrings:Default is used. |
SinkTableName |
Sink is MSSql |
Table name. Defaults to Logs. The table is created if it does not exist. |
Path, PathFormat |
File writer | File path and rolling path format used by the embedded file sink. |
FileSizeLimitBytes |
File writer | Default is 1073741824 (1 GB). |
RetainedFileCountLimit |
File writer | Default is 10. |
ApplicationName |
Always | Added to the Serilog properties. |
IsLogSessionInfo |
Optional | Stored in AppSettings for session-info logging. |
UseLogger() reads appsettings.json and appsettings.{Environment}.json from the application base directory.
Usage
Aspect-Oriented Logging
Add [Logged] to any method (controller or service). The attribute is in the global namespace, so no extra using is required. AspectType lives in Library.Logging.Enums.
using Library.Logging.Enums;
using Microsoft.AspNetCore.Mvc;
public class LoggingTestController : ControllerBase
{
public LoggingTestController() {}
[Logged]
[Route("Sum")]
[HttpGet]
public int? Sum(int a, int b)
{
return a + b;
}
// Optional: log only exceptions
[Logged(AspectType.OnException)]
[HttpPost("save")]
public IActionResult Save(Order order)
{
// ...
return Ok();
}
}
- No other logging code is required on the method. Entry, successful completion (including the return value) and exceptions are written automatically.
- Constructors:
[Logged],[Logged(AspectType)], and a third overload that also takes aFlowBehaviorand an optional return value. AspectTypevalues:OnEntry,OnExit,OnException,All(the default).
[Logged] also supports methods that return Task or Task<T>: on exit, the task is awaited and, for Task<T>, the result is logged.
Manual Logging
You can also write log messages directly:
using Library.Logging.Utils;
Logger.LogInformation("Custom info message...");
Logger.LogError("This went wrong!");
Logger.LogWarning("Something to watch");
Logger.LogFatal("Unrecoverable error");
Logger.LogTrace("Includes the calling method's name and type in the title");
Logger.CloseAndFlush();
LogInformation, LogError, LogWarning and LogFatal each take a message and an optional title string (not an exception object). Log is the general entry point: Logger.Log(message, LogType.Error, title).
Supported Sinks
- Graylog: the default. Set
"Sink": "Graylog"and provideServerandPort. Transport is UDP. - Microsoft SQL Server: set
"Sink": "MSSql"and provideSinkDBConnectionString(or aLogsDb/Defaultconnection string) and optionallySinkTableName. When MSSql is selected, the Graylog writer is not used. - File and console: the embedded Serilog template always includes a console writer and a file writer. File output uses
Path,PathFormat,FileSizeLimitBytesandRetainedFileCountLimit.
Test Results
Logs can be inspected in the Graylog UI, in the SQL table, on the console, or in the file.
Each
[Logged]entry includes:- Method and parameters
- Return value (or exception message and stack trace)
- User and request information, when
HttpContextis available on the instance - Timestamp
Example:
The LoggingTestController.Sum method started working with "[4,7]" parameters. The LoggingTestController.Sum method successfully completed with input ("[4,7]") and returned (11). ExecutionTimeMs: 1ms
Examples & Tests
Working samples, unit tests, load tests and benchmarks for CHD packages are available in the public Chd.Examples repository.
Authors
See also other contributors on NuGet.
Acknowledgements
- Graylog
- MethodBoundaryAspect.Fody
- Thanks for using this library — issues and PRs are welcome.
For issues or feature requests, visit mehmet-yoldas/Chd.Examples
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net8.0
- Chd.Library.Common (>= 8.7.3)
- MethodBoundaryAspect.Fody (>= 2.0.150)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.RazorPages (>= 2.2.5)
- Microsoft.AspNetCore.Mvc.ViewFeatures (>= 2.2.0)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- Serilog (>= 4.1.0)
- Serilog.Enrichers.Environment (>= 2.3.0)
- Serilog.Settings.Configuration (>= 8.0.0)
- Serilog.Sinks.File (>= 5.0.0)
- Serilog.Sinks.Graylog (>= 3.1.1)
- Serilog.Sinks.MSSqlServer (>= 8.2.2)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Chd.Library.Logging:
| Package | Downloads |
|---|---|
|
Chd.Library.Migrations
Chd (Cleverly Handle Difficulty) packages are easy to use. This package contains database migration utilities and helpers for managing schema updates, seeding, and migration tasks. Supports: MSSQL, Oracle, PostgreSQL, SQLite |
|
|
Chd.Library.Core
The library written for .NET Core framework |
|
|
Chd.Library.Web.Socket
Chd (Cleverly Handle Difficulty) packages are easy to use. This package contains webSocket helpers and server/client abstractions for real-time messaging and socket-based communications. |
|
|
Chd.Library.MQTT
Chd (Cleverly Handle Difficulty) packages are easy to use. This package contains MQTT helpers and client wrappers for IoT and lightweight pub/sub messaging scenarios. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.7.2 | 41 | 11/12/2026 |
| 8.7.1 | 241 | 9/5/2026 |
| 8.7.0 | 284 | 3/5/2026 |
| 8.6.6 | 232 | 2/19/2026 |
| 8.6.5 | 202 | 1/20/2026 |
| 8.6.4 | 249 | 1/15/2026 |
| 8.6.3 | 212 | 1/12/2026 |
| 8.6.2 | 496 | 12/20/2025 |
| 8.6.1 | 396 | 12/19/2025 |
| 8.6.0 | 340 | 12/19/2025 |
| 8.5.9 | 353 | 12/18/2025 |
| 8.5.7 | 348 | 12/18/2025 |
| 8.5.6 | 559 | 12/18/2025 |
| 8.5.5 | 352 | 12/18/2025 |
| 8.1.8 | 397 | 12/17/2025 |