SimplyWorks.Logger.ElasticSearch
10.0.1
See the version list below for details.
dotnet add package SimplyWorks.Logger.ElasticSearch --version 10.0.1
NuGet\Install-Package SimplyWorks.Logger.ElasticSearch -Version 10.0.1
<PackageReference Include="SimplyWorks.Logger.ElasticSearch" Version="10.0.1" />
<PackageVersion Include="SimplyWorks.Logger.ElasticSearch" Version="10.0.1" />
<PackageReference Include="SimplyWorks.Logger.ElasticSearch" />
paket add SimplyWorks.Logger.ElasticSearch --version 10.0.1
#r "nuget: SimplyWorks.Logger.ElasticSearch, 10.0.1"
#:package SimplyWorks.Logger.ElasticSearch@10.0.1
#addin nuget:?package=SimplyWorks.Logger.ElasticSearch&version=10.0.1
#tool nuget:?package=SimplyWorks.Logger.ElasticSearch&version=10.0.1
SW-Logger
| Package | Version | Downloads |
|---|---|---|
| SimplyWorks.Logger.ElasticSearch | ||
| SimplyWorks.Logger.Console |
Overview
SW-Logger is a .NET logging library built on top of Serilog that provides structured logging capabilities for .NET 8.0 applications. It offers two main logging sinks:
- Console Logger: Provides enhanced console logging with structured output and request context enrichment
- ElasticSearch Logger: Enables logging to ElasticSearch with data streams, lifecycle policies, and correlation tracking
Features
- 🎯 Structured Logging: Built on Serilog for structured, searchable logs
- 🔍 Request Context Enrichment: Automatically enriches logs with correlation IDs and user information
- 🗂️ ElasticSearch Integration: Direct logging to ElasticSearch with data streams and index lifecycle management
- 📊 Console Logging: Enhanced console output with JSON formatting in production
- ⚙️ Configuration-Driven: Flexible configuration through
appsettings.json - 🔄 Request Logging: Built-in HTTP request/response logging
- 🏷️ Environment Support: Environment-specific logging configurations
Installation
Console Logger
dotnet add package SimplyWorks.Logger.Console
ElasticSearch Logger
dotnet add package SimplyWorks.Logger.ElasticSearch
Quick Start
Console Logger Setup
1. Configure Services
using SW.Logger.Console;
public void ConfigureServices(IServiceCollection services)
{
services.AddSWConsoleLogger(options =>
{
options.ApplicationName = "MyApp";
options.LoggingLevel = 2; // Information (the default)
});
}
2. Configure Application Pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSWConsoleLogger();
// ... other middleware
}
ElasticSearch Logger Setup
1. Configure Host Builder
using SW.Logger.ElasticSerach;
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSwElasticSearchLogger(options =>
{
options.ApplicationName = "MyApp";
options.ElasticsearchUrl = "https://localhost:9200";
});
To add more Serilog sinks or enrichers, pass configureLogger; it runs after the built-in ones. Serilog replaces the .NET logging pipeline, so a logging provider such as Sentry's never receives these logs unless it is added here as a Serilog sink:
builder.Host.UseSwElasticSearchLogger(configureLogger: (context, loggerConfiguration) =>
loggerConfiguration.WriteTo.Sentry(o =>
{
o.InitializeSdk = false; // already initialized by UseSentry
o.MinimumEventLevel = LogEventLevel.Error;
}));
2. Configure Application Pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRequestContextLogEnricher();
// ... other middleware
}
Configuration
appsettings.json Configuration
{
"SwLogger": {
"ApplicationName": "MyApplication",
"LoggingLevel": 2,
"ElasticsearchUrl": "https://localhost:9200",
"ElasticsearchUser": "elastic",
"ElasticsearchPassword": "password",
"ElasticsearchEnvironments": "Development,Staging,Production",
"ElasticsearchCertificatePath": "/path/to/certificate.crt",
"ElasticsearchDeleteIndexAfterDays": 90
}
}
Configuration Options
Console Logger Options
ApplicationName: Name of your application (default: "unknownapp")ApplicationVersion: Version of your application (auto-detected from assembly)LoggingLevel: Minimum level as Serilog'sLogEventLevelnumber: 0=Verbose, 1=Debug, 2=Information (default), 3=Warning, 4=Error, 5=Fatal.Microsoft.AspNetCoreandMicrosoft.EntityFrameworkCoreare always held at Warning.Environments: Comma-separated list of environments where logging is activeLogRequests: Write one line per HTTP request with its duration, status and aRouteproperty holding the route template (/api/orders/{id}), so requests can be grouped per endpoint. Kept even whenLoggingLevelis above Information. Defaulttrue.SlowQueryMilliseconds: EF Core commands taking at least this long are logged ("Executed DbCommand (Nms)"), so a slow request can be traced to its query; faster ones are not logged. Failed commands always are.0disables. Default500.QuietRequestPaths: Comma-separated path prefixes whose successful requests are dropped (default/health,/healthz,/metrics). Failures are still logged.
Upgrading: before this change
LoggingLeveldefaulted to 1, which is Debug, not Information as this README claimed. Apps that never set it now log at Information, and framework chatter from ASP.NET Core and EF Core is Warning-only.
ElasticSearch Logger Options
ApplicationName: Name of your application (used for index naming)ApplicationVersion: Version of your applicationLoggingLevel: Serilog logging levelElasticsearchUrl: ElasticSearch cluster URLElasticsearchUser: ElasticSearch usernameElasticsearchPassword: ElasticSearch passwordElasticsearchEnvironments: Environments where ElasticSearch logging is enabledElasticsearchCertificatePath: Path to SSL certificate (optional)ElasticsearchDeleteIndexAfterDays: Days logs are kept before being deleted (default: 90). Set to0to keep logs forever.ElasticsearchReplicasAfterRollover: Replica count for each monthly backing index once it has rolled over and is no longer written to.0halves the disk older logs use; the index currently being written keeps its replicas, so node restarts never stop logging. Not set by default (replicas unchanged).MinimumLevelOverrides: Minimum level per log source, overridingLoggingLevelfor that source and everything under it. Levels are Serilog's: 0 Verbose, 1 Debug, 2 Information, 3 Warning, 4 Error, 5 Fatal. For example, to stop shipping one line per database query:"MinimumLevelOverrides": { "Microsoft.EntityFrameworkCore": 3, "Microsoft.AspNetCore": 3 }
Usage Examples
Basic Logging
using Microsoft.Extensions.Logging;
public class HomeController : ControllerBase
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Get()
{
_logger.LogInformation("Processing GET request for Home");
try
{
// Your logic here
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing GET request");
throw;
}
}
}
Structured Logging with Context
_logger.LogInformation("User {UserId} accessed resource {ResourceId}",
userId, resourceId);
_logger.LogWarning("Failed login attempt for {Username} from {IpAddress}",
username, ipAddress);
Project Structure
SW-Logger/
├── SW.Logger.Console/ # Console logging package
│ ├── IAppBuilderExtensions.cs
│ ├── IServiceCollectionExtensions.cs
│ └── LoggerOptions.cs
├── SW.Logger.ElasticSearch/ # ElasticSearch logging package
│ ├── IAppBuilderExtensions.cs
│ ├── IHostBuilderExtensions.cs
│ ├── LoggerOptions.cs
│ └── StringExtensions.cs
└── SW.Logger.SampleWeb/ # Sample web application
├── Program.cs
├── Startup.cs
└── appsettings.json
ElasticSearch Features
Data Streams
The ElasticSearch logger uses data streams with the naming pattern: logs-{application-name}-{environment}
Index Lifecycle Management
On startup, and every 24 hours after, the logger creates an ILM policy named logs-{application-name}-{environment}-retention
and applies it to its data stream. The policy rolls the data stream over monthly (or at 50 GB per shard) and deletes data
ElasticsearchDeleteIndexAfterDays after rollover. Because it's applied to the data stream itself rather than through an
index template, it isn't affected by which index template wins for the stream.
Authentication
Supports both basic authentication and certificate-based authentication for ElasticSearch clusters.
Dependencies
Console Logger
- Serilog.AspNetCore (8.0.3)
- SimplyWorks.PrimitiveTypes (8.0.0)
ElasticSearch Logger
- Elastic.Serilog.Sinks (8.18.2)
- NEST (7.17.5)
- Serilog.AspNetCore (8.0.3)
- SimplyWorks.PrimitiveTypes (8.0.0)
Requirements
- .NET 8.0 or higher
- For ElasticSearch logging: ElasticSearch cluster (7.x or 8.x)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any bugs or have feature requests, please submit an issue on GitHub.
| 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
- Elastic.Serilog.Sinks (>= 8.18.2)
- NEST (>= 7.17.5)
- Serilog.AspNetCore (>= 10.0.0)
- SimplyWorks.PrimitiveTypes (>= 10.0.1)
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.3 | 116 | 10/31/2026 |
| 10.0.2 | 96 | 10/30/2026 |
| 10.0.1 | 107 | 10/25/2026 |
| 10.0.0 | 288 | 10/11/2026 |
| 8.1.10 | 145 | 10/11/2026 |
| 8.1.9 | 117 | 10/5/2026 |
| 8.1.8 | 151 | 10/4/2026 |
| 8.1.7 | 115 | 9/29/2026 |
| 8.1.6 | 695 | 8/26/2026 |
| 8.1.5 | 230 | 7/14/2026 |
| 8.1.4 | 221 | 7/13/2026 |
| 8.1.3 | 223 | 7/13/2026 |
| 8.1.2 | 256 | 6/25/2026 |
| 8.1.1 | 1,435 | 1/23/2026 |
| 8.1.0 | 372 | 9/16/2025 |
| 8.0.12 | 721 | 7/8/2025 |
| 8.0.10 | 193 | 6/22/2025 |
| 8.0.6 | 196 | 2/3/2025 |
| 8.0.5 | 182 | 2/3/2025 |