CG.Infrastructure.Logging 3.10.3

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

Infrastructure.Logging

A comprehensive logging library for .NET applications built on top of Serilog, providing structured logging capabilities with extensive configuration options and multiple output sinks.

Overview

Infrastructure.Logging is a .NET 9.0 library that simplifies logging implementation across your applications. It provides a unified logging interface with Serilog as the underlying engine, offering structured logging, correlation ID tracking, and multiple output destinations.

Features

  • Structured Logging: Leverage Serilog's powerful structured logging capabilities
  • Multiple Output Sinks: Console, file, HTTP, and SQL Server logging support
  • Correlation ID Tracking: Automatically track request correlation across log entries
  • Environment Enrichment: Automatically include environment and process information
  • Exception Handling: Enhanced exception logging with detailed context
  • Expression-based Filtering: Advanced log filtering using Serilog expressions
  • Browser Console Support: Client-side logging for web applications
  • Configuration-based Setup: Easy configuration through appsettings.json

Target Framework

  • .NET 9.0

Dependencies

Core Dependencies

  • CG.Infrastructure.Core (3.10.7) - Core infrastructure components
  • Serilog (4.3.0) - Structured logging framework

ASP.NET Core Integration

  • Serilog.AspNetCore (9.0.0) - ASP.NET Core integration
  • Serilog.Extensions.Hosting (9.0.0) - Hosting integration

Enrichers

  • Serilog.Enrichers.CorrelationId (3.0.1) - Request correlation tracking
  • Serilog.Enrichers.Environment (3.0.1) - Environment information
  • Serilog.Enrichers.Process (3.0.0) - Process information
  • Serilog.Enrichers.Thread (4.0.0) - Thread information

Advanced Features

  • Serilog.Exceptions (8.4.0) - Enhanced exception logging
  • Serilog.Expressions (5.0.0) - Expression-based filtering

Output Sinks

  • Serilog.Sinks.Console (6.0.0) - Console output
  • Serilog.Sinks.File (7.0.0) - File output
  • Serilog.Sinks.Http (9.2.0) - HTTP endpoint output
  • Serilog.Sinks.MSSqlServer (8.2.2) - SQL Server database output
  • Serilog.Sinks.BrowserConsole (8.0.0) - Browser console output

Configuration

  • Serilog.Settings.Configuration (9.0.0) - Configuration-based setup

Utilities

  • System.Text.Json (9.0.8) - JSON serialization support

Package Information

  • Package ID: CG.Infrastructure.Logging
  • Version: 3.10.2
  • Authors: Matthew Evans
  • Company: Matthew Evans
  • Product: CG.Infrastructure.Logging
  • Description: Infra Logging library with Serilog setup, extensions and database contexts

Getting Started

Installation

dotnet add package CG.Infrastructure.Logging

Basic Configuration

// Program.cs or Startup.cs
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Configure Serilog
builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("logs/app-.txt", rollingInterval: RollingInterval.Day));

Configuration Example (appsettings.json)

{
  "Serilog": {
    "Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File"],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "logs/app-.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7
        }
      }
    ],
    "Enrich": ["FromLogContext", "WithCorrelationId", "WithEnvironment", "WithProcess", "WithThread"]
  }
}

Usage Examples

Basic Logging

using Serilog;

public class ExampleService
{
    private readonly ILogger _logger;

    public ExampleService(ILogger logger)
    {
        _logger = logger;
    }

    public void ProcessData(string data)
    {
        _logger.Information("Processing data: {Data}", data);
        
        try
        {
            // Process data
            _logger.Information("Data processed successfully");
        }
        catch (Exception ex)
        {
            _logger.Error(ex, "Error processing data: {Data}", data);
            throw;
        }
    }
}

Structured Logging

_logger.Information("User {UserId} accessed {Resource} from {IPAddress}", 
    userId, resourceName, ipAddress);

Correlation ID Tracking

// Automatically included when using Serilog.Enrichers.CorrelationId
_logger.Information("Request processed");
// Output: [CorrelationId: abc123] Request processed

Advanced Features

Expression-based Filtering

{
  "Serilog": {
    "Filter": [
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "RequestPath like '/health%'"
        }
      }
    ]
  }
}

SQL Server Logging

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=...;Database=Logs;...",
          "tableName": "Logs",
          "autoCreateSqlTable": true
        }
      }
    ]
  }
}

HTTP Sink

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Http",
        "Args": {
          "requestUri": "https://logs.example.com/api/logs",
          "queueLimitBytes": null
        }
      }
    ]
  }
}

Best Practices

  1. Use Structured Logging: Pass objects as properties rather than concatenating strings
  2. Configure Appropriate Log Levels: Set minimum levels based on environment
  3. Include Correlation IDs: Enable correlation ID tracking for distributed systems
  4. Use Async Logging: Configure sinks to handle high-volume logging asynchronously
  5. Monitor Log Performance: Use appropriate sink configurations for production environments

Contributing

This project is part of the CG Infrastructure Libraries. For contributions, please refer to the main infrastructure repository guidelines.

License

See the LICENSE file in the root directory for licensing information.

Support

For issues and questions related to this library, please contact the development team or create an issue in the project repository.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on CG.Infrastructure.Logging:

Package Downloads
CG.Platform.ViewModels

Platform view models library with shared services

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.10.3 11 8/10/2025
3.10.2 109 7/11/2025
3.10.1 144 6/18/2025
3.10.0 124 6/7/2025
3.9.0 133 12/10/2024
3.0.5 147 7/12/2024
3.0.4 178 3/19/2024
3.0.3 155 3/19/2024
3.0.2 141 3/19/2024
3.0.1 144 2/26/2024
3.0.0 165 2/19/2024
2.0.0 357 5/15/2023
1.0.2 613 5/27/2022
1.0.1 574 5/27/2022
1.0.0 579 5/26/2022