SimplyWorks.Logger.ElasticSearch 8.1.0

dotnet add package SimplyWorks.Logger.ElasticSearch --version 8.1.0
                    
NuGet\Install-Package SimplyWorks.Logger.ElasticSearch -Version 8.1.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="SimplyWorks.Logger.ElasticSearch" Version="8.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimplyWorks.Logger.ElasticSearch" Version="8.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SimplyWorks.Logger.ElasticSearch" />
                    
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 SimplyWorks.Logger.ElasticSearch --version 8.1.0
                    
#r "nuget: SimplyWorks.Logger.ElasticSearch, 8.1.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 SimplyWorks.Logger.ElasticSearch@8.1.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=SimplyWorks.Logger.ElasticSearch&version=8.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SimplyWorks.Logger.ElasticSearch&version=8.1.0
                    
Install as a Cake Tool

SW-Logger

Build and Publish NuGet Package License: MIT

Package Version Downloads
SimplyWorks.Logger.ElasticSearch Nuget Nuget
SimplyWorks.Logger.Console Nuget Nuget

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 = 1; // Information level
    });
}
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";
        });
2. Configure Application Pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRequestContextLogEnricher();
    // ... other middleware
}

Configuration

appsettings.json Configuration

{
  "SwLogger": {
    "ApplicationName": "MyApplication",
    "LoggingLevel": 1,
    "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: Serilog logging level (1=Information, 2=Debug, etc.)
  • Environments: Comma-separated list of environments where logging is active
ElasticSearch Logger Options
  • ApplicationName: Name of your application (used for index naming)
  • ApplicationVersion: Version of your application
  • LoggingLevel: Serilog logging level
  • ElasticsearchUrl: ElasticSearch cluster URL
  • ElasticsearchUser: ElasticSearch username
  • ElasticsearchPassword: ElasticSearch password
  • ElasticsearchEnvironments: Environments where ElasticSearch logging is enabled
  • ElasticsearchCertificatePath: Path to SSL certificate (optional)
  • ElasticsearchDeleteIndexAfterDays: Days after which indices are deleted (default: 90)

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

Automatically creates and applies lifecycle policies that delete indices after the configured retention period.

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 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. 
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
8.1.0 258 9/16/2025
8.0.12 328 7/8/2025
8.0.10 109 6/22/2025
8.0.6 113 2/3/2025
8.0.5 100 2/3/2025