Serilog.Enrichers.SqlException
0.0.6
See the version list below for details.
dotnet add package Serilog.Enrichers.SqlException --version 0.0.6
NuGet\Install-Package Serilog.Enrichers.SqlException -Version 0.0.6
<PackageReference Include="Serilog.Enrichers.SqlException" Version="0.0.6" />
<PackageVersion Include="Serilog.Enrichers.SqlException" Version="0.0.6" />
<PackageReference Include="Serilog.Enrichers.SqlException" />
paket add Serilog.Enrichers.SqlException --version 0.0.6
#r "nuget: Serilog.Enrichers.SqlException, 0.0.6"
#:package Serilog.Enrichers.SqlException@0.0.6
#addin nuget:?package=Serilog.Enrichers.SqlException&version=0.0.6
#tool nuget:?package=Serilog.Enrichers.SqlException&version=0.0.6
Serilog.Enrichers.SqlException
A Serilog enricher that extracts structured properties from Microsoft.Data.SqlClient.SqlException and adds them to log events. Enhance observability and troubleshooting for .NET applications using SQL Server by capturing detailed exception metadata such as error number, state, severity, procedure name, and line number.
Features
- Automatic SqlException Detection: Walks the exception chain to find
SqlExceptioninstances, even when wrapped in other exception types - Structured Logging: Extracts key properties as separate log event properties for filtering and querying
- Circular Reference Protection: Safely handles circular exception chains
- Zero Configuration: Works out of the box with a single fluent API call
- Broad Compatibility: Targets .NET Standard 2.0 for wide framework support
Installation
Install via NuGet Package Manager:
Install-Package Serilog.Enrichers.SqlException
Or via .NET CLI:
dotnet add package Serilog.Enrichers.SqlException
Quick Start
Add the enricher to your Serilog configuration:
using Serilog;
Log.Logger = new LoggerConfiguration()
.Enrich.WithSqlExceptionEnricher()
.WriteTo.Console()
.CreateLogger();
try
{
// Your SQL operations
}
catch (Exception ex)
{
Log.Error(ex, "Database operation failed");
}
Enriched Properties
When a SqlException is detected, the following properties are added to the log event:
| Property | Type | Description | Always Present |
|---|---|---|---|
SqlException_IsSqlException |
bool |
Indicates a SqlException was found | Yes |
SqlException_Number |
int |
SQL Server error number | Yes |
SqlException_State |
byte |
Error state | Yes |
SqlException_Class |
byte |
Severity/class level (1-25) | Yes |
SqlException_Line |
int |
Line number where error occurred | Yes |
SqlException_Procedure |
string |
Stored procedure name | Conditional* |
SqlException_Server |
string |
Server name | Conditional* |
SqlException_Message |
string |
Error message | Conditional* |
* Only included if non-empty
Example Log Output
{
"@t": "2024-12-09T10:30:15.1234567Z",
"@mt": "Database operation failed",
"@l": "Error",
"@x": "Microsoft.Data.SqlClient.SqlException...",
"SqlException_IsSqlException": true,
"SqlException_Number": 1205,
"SqlException_State": 13,
"SqlException_Class": 20,
"SqlException_Procedure": "sp_UpdateInventory",
"SqlException_Line": 42,
"SqlException_Server": "sql-prod-01",
"SqlException_Message": "Transaction was deadlocked on lock resources with another process"
}
How It Works
The enricher implements ILogEventEnricher and performs the following operations:
- Exception Chain Traversal: Walks through
InnerExceptionreferences to locateSqlExceptioninstances - Circular Reference Protection: Uses a
HashSet<Exception>to detect and break circular chains - Property Extraction: Extracts properties from the first
SqlErrorin theSqlException.Errorscollection - Conditional Enrichment: Only adds properties if they contain meaningful values (non-null, non-empty)
- Safe Property Addition: Uses
AddPropertyIfAbsent()to avoid overwriting existing properties
Advanced Usage
Combining with Other Enrichers
Log.Logger = new LoggerConfiguration()
.Enrich.WithThreadId()
.Enrich.WithMachineName()
.Enrich.WithSqlExceptionEnricher()
.Enrich.FromLogContext()
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();
Filtering by SQL Error Number
Log.Logger = new LoggerConfiguration()
.Enrich.WithSqlExceptionEnricher()
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(le =>
le.Properties.TryGetValue("SqlException_Number", out var number) &&
number.ToString() == "1205") // Deadlock errors only
.WriteTo.Seq("http://localhost:5341"))
.CreateLogger();
Querying Enriched Logs in Seq
-- Find all deadlock errors
SqlException_Number = 1205
-- Find errors in specific stored procedure
SqlException_Procedure = 'sp_UpdateInventory'
-- Find high-severity errors (16+)
SqlException_Class >= 16
Common SQL Error Numbers
| Error | Description |
|---|---|
| 1205 | Deadlock victim |
| 2627 | Unique constraint violation |
| 547 | Foreign key constraint violation |
| 515 | Cannot insert NULL |
| 8152 | String or binary data truncated |
Requirements
- .NET Standard 2.0+ (compatible with .NET Framework 4.6.1+, .NET Core 2.0+, .NET 5+)
- Serilog ≥ 2.12.0
- Microsoft.Data.SqlClient ≥ 5.2.0
Building from Source
# Clone the repository
git clone https://github.com/brandonhenricks/serilog-enrichers-sqlexception.git
cd serilog-enrichers-sqlexception
# Restore dependencies
dotnet restore
# Build the solution
dotnet build -c Release
# Run tests
dotnet test
# Create NuGet package
dotnet pack -c Release
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Commit your changes with clear messages
- Add or update tests as needed
- Ensure all tests pass (
dotnet test) - Submit a pull request
Testing
The test suite uses xUnit and includes scenarios for:
- SqlException with all properties populated
- SqlException as inner exception
- Non-SqlException in chain
- Empty/null optional properties
- Circular exception reference protection
Run tests with code coverage:
dotnet test --collect:"XPlat Code Coverage"
License
This project is licensed under the MIT License.
Acknowledgments
Built with Serilog - flexible, structured logging for .NET.
Support
- Issues: GitHub Issues
- Documentation: Serilog Documentation
- NuGet: Serilog.Enrichers.SqlException
Made with ❤️ by Brandon Henricks
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Data.SqlClient (>= 5.2.0)
- Serilog (>= 2.12.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.