CSharpEssentials.LoggerHelper.Sink.MSSqlServer 5.2.2.5

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

CSharpEssentials.LoggerHelper.Sink.MSSqlServer

SQL Server structured log storage with auto table creation and custom columns for CSharpEssentials.LoggerHelper.

Targets: net8.0 · net9.0 · net10.0 — Part of the CSharpEssentials.LoggerHelper ecosystem. Install only the sinks you need.


Install

dotnet add package CSharpEssentials.LoggerHelper
dotnet add package CSharpEssentials.LoggerHelper.Sink.MSSqlServer

Quick Setup — JSON

Add to appsettings.json:

{
  "LoggerHelper": {
    "ApplicationName": "MyApp",
    "Routes": [
      { "Sink": "MSSqlServer", "Levels": ["Warning", "Error", "Fatal"] }
    ],
    "Sinks": {
      "MSSqlServer": {
        "ConnectionString": "Server=.;Database=Logs;Trusted_Connection=true;TrustServerCertificate=true",
        "TableName": "AppLogs",
        "AutoCreateSqlTable": true,
        "BatchPostingLimit": 100,
        "Period": "0.00:00:10"
      }
    }
  }
}
// Program.cs
builder.Services.AddLoggerHelper(builder.Configuration);

var app = builder.Build();
app.UseLoggerHelper();   // ← required: activates sinks and registers middleware

AutoCreateSqlTable: true creates the table on first run if it does not already exist. Set to false if you manage schema migrations yourself.


Quick Setup — Fluent API

builder.Services.AddLoggerHelper(b => b
    .WithApplicationName("MyApp")
    .AddRoute("MSSqlServer", LogEventLevel.Warning, LogEventLevel.Error, LogEventLevel.Fatal)
    .ConfigureMSSqlServer(s => {
        s.ConnectionString  = "Server=.;Database=Logs;Trusted_Connection=true;TrustServerCertificate=true";
        s.TableName         = "AppLogs";
        s.AutoCreateSqlTable = true;
    })
);

var app = builder.Build();
app.UseLoggerHelper();   // ← required

What You'll See

Log events are batched and inserted as rows into the configured table. Default columns created by AutoCreateSqlTable:

Column SQL Type Notes
Id BIGINT IDENTITY Primary key
Message NVARCHAR(MAX) Rendered log message
MessageTemplate NVARCHAR(MAX) Raw template with {placeholders}
Level NVARCHAR(128) e.g. Warning, Error
TimeStamp DATETIME UTC timestamp
Exception NVARCHAR(MAX) Full exception string (nullable)
Properties NVARCHAR(MAX) All structured properties as XML

Custom Columns — Map Log Properties to SQL Columns

Use AdditionalColumns to promote any log property into a dedicated, queryable column.

"Sinks": {
  "MSSqlServer": {
    "ConnectionString": "...",
    "TableName": "AppLogs",
    "AdditionalColumns": [
      { "ColumnName": "TenantId",   "DataType": "NVarChar", "DataLength": 100, "AllowNull": true },
      { "ColumnName": "RequestId",  "DataType": "NVarChar", "DataLength": 50,  "AllowNull": true },
      { "ColumnName": "UserId",     "DataType": "NVarChar", "DataLength": 50,  "AllowNull": true }
    ]
  }
}

Then populate those properties at runtime using BeginScope or LogContext.PushProperty:

// Option A — BeginScope (preferred for scoped operations)
using (_logger.BeginScope(new Dictionary<string, object?> {
    ["TenantId"]  = "acme",
    ["RequestId"] = HttpContext.TraceIdentifier
}))
{
    _logger.LogWarning("Payment failed for order {OrderId}", orderId);
}

// Option B — LogContext (Serilog-specific)
using (Serilog.Context.LogContext.PushProperty("UserId", userId))
{
    _logger.LogError("Unauthorized access attempt");
}

The ColumnName must exactly match the log property name (case-insensitive). The column is created automatically when AutoCreateSqlTable: true.


Configuration Options

Property Type Default Description
ConnectionString string "" Required. ADO.NET SQL Server connection string.
TableName string "Logs" Target table name.
SchemaName string "dbo" Table schema.
AutoCreateSqlTable bool true Create the table on startup if it does not exist.
BatchPostingLimit int 100 Maximum events per batch INSERT.
Period string "0.00:00:10" Flush interval in d.hh:mm:ss format. "0.00:00:10" = 10 seconds.
AddStandardColumns List<string>? null Standard columns to include. Valid values: Id, Message, MessageTemplate, Level, TimeStamp, Exception, Properties, LogEvent.
RemoveStandardColumns List<string>? null Standard columns to exclude (e.g. ["Properties"] to drop the XML blob).
AdditionalColumns List<AdditionalColumnConfig>? null Custom columns mapped from log properties (see above).

AdditionalColumnConfig

Property Type Default Description
ColumnName string "" SQL column name — must match the log property name.
DataType string "NVarChar" SQL type. Any SqlDbType name: NVarChar, Int, BigInt, DateTime, Bit, etc.
AllowNull bool true Whether the column accepts NULL.
DataLength int -1 Column length. -1 = MAX. Use a fixed length (e.g. 100) for indexed columns.

Troubleshooting

Symptom Likely Cause Fix
No output at all app.UseLoggerHelper() missing Add it after builder.Build()
Table not created Insufficient DB permissions or AutoCreateSqlTable: false Grant CREATE TABLE permission or create the table manually
Rows appear with delay Events are batched — flushed every Period or when BatchPostingLimit is reached Reduce Period (e.g. "0.00:00:02") or BatchPostingLimit for faster writes
AdditionalColumns column always NULL Log property name doesn't match ColumnName Check casing: TenantId in config ↔ ["TenantId"] in BeginScope
TrustServerCertificate error SQL Server uses a self-signed certificate Add TrustServerCertificate=true to the connection string

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 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 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
5.2.2.5 28 7/5/2026
5.2.2.4 28 7/5/2026
5.2.2.3 37 7/5/2026
5.2.2.2 44 7/5/2026
5.2.2.1 39 7/4/2026
5.2.2 49 7/3/2026
5.2.0 96 6/29/2026
5.1.1 102 6/19/2026
5.1.0 105 6/16/2026
5.0.8 107 6/13/2026
5.0.7 101 6/11/2026
5.0.6 93 6/10/2026
5.0.5 105 6/6/2026
5.0.4 104 6/5/2026
5.0.3 105 6/2/2026
5.0.2 107 6/1/2026
5.0.1 108 5/31/2026
5.0.0 104 5/31/2026
4.0.2.2 223 10/10/2025
4.0.2.1 259 9/13/2025
Loading failed