JAC.Framework.Logging.Serilog
1.0.1
dotnet add package JAC.Framework.Logging.Serilog --version 1.0.1
NuGet\Install-Package JAC.Framework.Logging.Serilog -Version 1.0.1
<PackageReference Include="JAC.Framework.Logging.Serilog" Version="1.0.1" />
<PackageVersion Include="JAC.Framework.Logging.Serilog" Version="1.0.1" />
<PackageReference Include="JAC.Framework.Logging.Serilog" />
paket add JAC.Framework.Logging.Serilog --version 1.0.1
#r "nuget: JAC.Framework.Logging.Serilog, 1.0.1"
#:package JAC.Framework.Logging.Serilog@1.0.1
#addin nuget:?package=JAC.Framework.Logging.Serilog&version=1.0.1
#tool nuget:?package=JAC.Framework.Logging.Serilog&version=1.0.1
Features
- 🚀 Easy integration - Simple extension methods for registering Serilog with
IServiceCollection - 🔧 Flexible configuration - Supports both
appsettings.json-based and code-based setup - 📝 Built-in enrichment - Adds machine name, thread ID, and log context enrichment
- 📄 Ready-to-use sinks - Console and file logging available out of the box
- 🎯 Consistent logging - Standardized logging configuration across .NET applications
Installation
Install the NuGet package:
dotnet add package JAC.Framework.Logging.Serilog
Quick Start
Register logging in your application using one of the provided extension methods.
Using IConfiguration
Use this option when you want Serilog settings to come from appsettings.json or another .NET configuration provider.
using Framework.Logging.Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilogLogging( builder.Configuration, applicationName: "MyApp");
Using the default code-based configuration
Use this option when you want a simple built-in setup with console and file logging.
csharp using Framework.Logging.Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilogLogging( logFilePath: "logs/app-.txt", applicationName: "MyApp");
JSON Configuration
The AddSerilogLogging(IServiceCollection, IConfiguration, string?) overload reads settings from the top-level Serilog section using ReadFrom.Configuration(...) [1].
Example appsettings.json
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async"],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{SourceContext}] [{Level:u3}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "logs/imdotnet-.log",
"rollingInterval": "Day",
"retainedFileCountLimit": 30,
"restrictedToMinimumLevel": "Warning",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] [{SourceContext}] [{Server}] {Message:lj}{NewLine}{Exception}"
}
}
]
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId"],
"Properties": {
"Application": "MyApp"
"Server": "Server: localhost"
}
}
}
Registration with configuration
csharp using Framework.Logging.Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilogLogging( builder.Configuration, applicationName: "MyApp");
Important notes
- The
Serilogsection must exist at the top level of the configuration source [1]. - The
Usingsection explicitly declares the sinks and enrichers used by JSON configuration [1]. - When
applicationNameis provided, the library enriches log events with an application-specific property in code. - JSON-based configuration is useful when you want to adjust log levels, sinks, or enrichment without changing source code.
Code-Based Configuration
The AddSerilogLogging(IServiceCollection, string, string?) overload creates a default logger with:
- Minimum level set to
Information Microsoftlogs overridden toWarningSystemlogs overridden toWarning- Log context enrichment enabled
- Machine name enrichment enabled
- Thread ID enrichment enabled
- Console sink enabled
- Rolling file sink enabled
Example
using Framework.Logging.Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilogLogging( logFilePath: "logs/app-.txt", applicationName: "MyApp");
Default Output Behavior
The built-in code-based configuration writes logs to:
- Console
- Rolling file logs using a daily interval
Default file path:
logs/app-{Date}.txt
Default file retention:
30 files
Enrichment
This package adds the following enrichers:
FromLogContextWithMachineNameWithThreadId
When an application name is provided, an application property is also attached to log events.
Example Usage
After registration, continue using the standard ILogger<T> abstraction from Microsoft.Extensions.Logging.
csharp using Microsoft.Extensions.Logging;
public sealed class Worker(ILogger<Worker> logger)
{
public void Run()
{
logger.LogInformation("Worker started at {StartedAt}", DateTimeOffset.UtcNow);
}
}
Requirements
- .NET 8.0
License
MIT
| Product | Versions 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. |
-
net10.0
- Microsoft.Extensions.Configuration (>= 10.0.4)
- Serilog (>= 4.3.1)
- Serilog.Enrichers.Environment (>= 3.0.1)
- Serilog.Enrichers.Thread (>= 4.0.0)
- Serilog.Extensions.Logging (>= 10.0.0)
- Serilog.Settings.Configuration (>= 10.0.0)
- Serilog.Sinks.Async (>= 2.1.0)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.