Pacem.Extensions.Logging.SqlServer 0.9.0-dirac

This is a prerelease version of Pacem.Extensions.Logging.SqlServer.
dotnet add package Pacem.Extensions.Logging.SqlServer --version 0.9.0-dirac
                    
NuGet\Install-Package Pacem.Extensions.Logging.SqlServer -Version 0.9.0-dirac
                    
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.9.0-dirac" />
                    
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.9.0-dirac" />
                    
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.9.0-dirac
                    
#r "nuget: Pacem.Extensions.Logging.SqlServer, 0.9.0-dirac"
                    
#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.9.0-dirac
                    
#: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.9.0-dirac&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Pacem.Extensions.Logging.SqlServer&version=0.9.0-dirac&prerelease
                    
Install as a Cake Tool

SqlServer Logging Extensions

⚗️ ==Experimental==

⚠️ Experimental libs are discouraged to use in production.

About

Pacem.Extensions.Loggin.SqlServer provides extensions to store your logs into a Sql Server 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 =>
    {
        if (!log.Name.StartsWith("Pacem."))
        {
            // by returning null you skip the log
            return null;
        }
        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
        };
    });

Plans for version next

Multiple entities log.

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

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.9.0-dirac 48 8/2/2025
0.9.0-cayley 94 7/31/2025
0.9.0-boyle 92 7/31/2025
0.9.0-abel 113 7/17/2025
0.8.11 163 5/27/2025
0.8.10 150 5/7/2025
0.8.10-fourier 137 5/2/2025
0.8.10-fibonacci 206 2/7/2025
0.8.10-fermat 99 2/3/2025
0.8.10-euler 94 2/3/2025
0.8.10-euclid 100 2/1/2025
0.8.10-dalembert 99 1/31/2025
0.8.10-cauchy 101 1/26/2025
0.8.10-cantor 107 12/18/2024
0.8.10-caccioppoli 178 12/6/2024
0.8.10-bourbaki 99 12/6/2024
0.8.10-abel 116 4/22/2024
0.8.6 336 3/17/2024
0.8.6-abel 106 3/14/2024
0.8.5 164 3/13/2024
0.8.4 124 3/12/2024
0.8.3 139 3/12/2024
0.8.2 137 3/11/2024
0.8.1 126 3/11/2024