Pacem.Extensions.Logging.SqlServer 0.10.12

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pacem.Extensions.Logging.SqlServer --version 0.10.12
                    
NuGet\Install-Package Pacem.Extensions.Logging.SqlServer -Version 0.10.12
                    
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="Pacem.Extensions.Logging.SqlServer" Version="0.10.12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pacem.Extensions.Logging.SqlServer" Version="0.10.12" />
                    
Directory.Packages.props
<PackageReference Include="Pacem.Extensions.Logging.SqlServer" />
                    
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 Pacem.Extensions.Logging.SqlServer --version 0.10.12
                    
#r "nuget: Pacem.Extensions.Logging.SqlServer, 0.10.12"
                    
#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 Pacem.Extensions.Logging.SqlServer@0.10.12
                    
#: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=Pacem.Extensions.Logging.SqlServer&version=0.10.12
                    
Install as a Cake Addin
#tool nuget:?package=Pacem.Extensions.Logging.SqlServer&version=0.10.12
                    
Install as a Cake Tool

SqlServer Logging Extensions

⚗️ ==Experimental==

⚠️ Experimental libs are discouraged to use in production.

About

Pacem.Extensions.Logging.SqlServer provides extensions to store your logs into a Sql Server database.

Rows are written by a background consumer, so logging calls never block on the database.

The following paragraphs exemplify the library utilization given common use cases.

How to

Add the relevant logger provider to the logging builder (e.g. ILoggingBuilder).

First, define the target entity.
Exploit DataAnnotation like in attribute-based entity definitions (if you're familiar with EntityFramework Core).

ℹ️ Please notice:

  • TableAttribute is recommended to correctly identify the target table.
    Fallback, in case of missing attribute, is dbo.<LogEntityTypeName>.
  • ColumnAttribute is mandatory in case of name mismatch between property and column, but optional otherwise.

If you want to ignore a property, decorate it with a NotMappedAttribute.

// Structure of a log row in the [Logging].[Logs] table in the database
[Table("Logs", Schema = "Logging")]
public class MyLogEntity
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
    public int Id { get; set; }

    [Required]
    public required string Source { get; set; }

    [Required]
    public required string Message { get; set; }

    [Column(TypeName = "tinyint")]
    public LogLevel LogLevel { get; set; }

    [Column(TypeName = "datetimeoffset(3)")]
    public DateTimeOffset Timestamp { get; set; }

    public string? ErrorMessage { get; set; }

    public string? ErrorStack { get; set; }

    public string? AllData { get; set; }

    /// <summary>
    /// Some relation to the business logic world "outside", if any.
    /// </summary>
    public Guid? RelationId { get; set; }
}

Then, configure the logger provider:

// Program.cs
builder.Logging.AddPacemSqlServerLogging<MyLogEntity>(
    "sql-server-connectionstring-here",
    // mapper: log line -> log entity
    log =>
    {
        Guid? relationId = default;
        if (log.Data?.TryGetValue("relation", out var relationObj) == true
        && Guid.TryParse(relationObj?.ToString(), out Guid result))
        {
            relationId = result;
        }
        return new MyLogEntity
        {
            Source = log.Name,
            Timestamp = log.Timestamp,
            Message = log.Message,
            ErrorMessage = log.Exception?.Message,
            ErrorStack = log.Exception?.StackTrace,
            LogLevel = log.LogLevel,
            AllData = JsonSerializer.Serialize(log.Data),
            RelationId = relationId
        };
    },
    // accumulate several rows into one INSERT (default: false)
    batch: true,
    // further options
    configure: options =>
    {
        // only persist our own categories
        options.Filter = log => log.Name.StartsWith("Pacem.");
    });

ℹ️ The mapper may also return null to skip an entry, but Filter is the preferred place for that decision: it runs earlier (on the caller's thread, before the row is queued) and keeps the mapper about mapping only.

The log payload

The SqlServerLogPayload handed to the mapper and the filter exposes:

Member Notes
Name Logger category.
Timestamp When the entry was produced.
LogLevel Severity.
Message Formatted message.
EventId The EventId passed to the logger, if any.
Exception The associated exception, if any.
Data The structured log values (message placeholders), as a dictionary.

Plans for version next

Multiple entities log.

Product 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. 
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
0.10.14-boltzmann 0 9/5/2026
0.10.13 45 9/3/2026
0.10.13-abel 37 9/3/2026
0.10.12 37 9/3/2026
0.10.11 82 8/26/2026
0.10.10 104 7/28/2026
0.10.8 113 5/26/2026
0.10.8-carnot 113 5/12/2026
0.10.8-cardano 103 5/11/2026
0.10.8-bombelli 94 5/6/2026
0.10.7 111 4/22/2026
0.10.7-rc 108 4/22/2026
0.10.0-descartes 122 1/19/2026
0.10.0-cramer 129 1/13/2026
0.10.0-cotes 121 1/13/2026
0.10.0-clapeyron 121 1/12/2026
0.10.0-cauchy 124 1/12/2026
0.10.0-boyle 130 12/30/2025
0.10.0-abel 217 12/4/2025
0.9.14 596 12/1/2025
Loading failed