diff --git a/README.md b/README.md index 656967a8..5fb12a13 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ This library has custom code to deal with extra properties on most common except [![NuGet Package](https://img.shields.io/nuget/v/Serilog.Exceptions.SqlServer.svg)](https://www.nuget.org/packages/Serilog.Exceptions.SqlServer/) [![Serilog.Exceptions.SqlServer package in serilog-exceptions feed in Azure Artifacts](https://feeds.dev.azure.com/serilog-exceptions/_apis/public/Packaging/Feeds/8479813c-da6b-4677-b40d-78df8725dc9c/Packages/67be830c-2c0f-4df8-be30-771d817b382f/Badge)](https://dev.azure.com/serilog-exceptions/Serilog.Exceptions/_packaging?_a=package&feed=8479813c-da6b-4677-b40d-78df8725dc9c&package=67be830c-2c0f-4df8-be30-771d817b382f&preferRelease=true) -Add the [Serilog.Exceptions.SqlServer](https://www.nuget.org/packages/Serilog.Exceptions.SqlServer/) NuGet package to your project to avoid the reflection based destructurer for `SqlException` +Add the [Serilog.Exceptions.SqlServer](https://www.nuget.org/packages/Serilog.Exceptions.SqlServer/) NuGet package to your project to avoid the reflection based destructurer for `SqlException` when using [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient/): ``` Install-Package Serilog.Exceptions.SqlServer @@ -110,6 +110,23 @@ Add the `SqlExceptionDestructurer` during setup: .WithDestructurers(new[] { new SqlExceptionDestructurer() })) ``` +### Serilog.Exceptions.MsSqlServer + +[![NuGet Package](https://img.shields.io/nuget/v/Serilog.Exceptions.MsSqlServer.svg)](https://www.nuget.org/packages/Serilog.Exceptions.MsSqlServer/) [![Serilog.Exceptions.MsSqlServer package in serilog-exceptions feed in Azure Artifacts](https://feeds.dev.azure.com/serilog-exceptions/_apis/public/Packaging/Feeds/8479813c-da6b-4677-b40d-78df8725dc9c/Packages/67be830c-2c0f-4df8-be30-771d817b382f/Badge)](https://dev.azure.com/serilog-exceptions/Serilog.Exceptions/_packaging?_a=package&feed=8479813c-da6b-4677-b40d-78df8725dc9c&package=67be830c-2c0f-4df8-be30-771d817b382f&preferRelease=true) + +Add the [Serilog.Exceptions.MsSqlServer](https://www.nuget.org/packages/Serilog.Exceptions.MsSqlServer/) NuGet package to your project to avoid the reflection based destructurer for `SqlException` when using [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient/): + +``` +Install-Package Serilog.Exceptions.MsSqlServer +``` + +Add the `SqlExceptionDestructurer` during setup: +```csharp +.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder() + .WithDefaultDestructurers() + .WithDestructurers(new[] { new SqlExceptionDestructurer() })) +``` + ### Serilog.Exceptions.EntityFrameworkCore [![NuGet Package](https://img.shields.io/nuget/v/Serilog.Exceptions.EntityFrameworkCore.svg)](https://www.nuget.org/packages/Serilog.Exceptions.EntityFrameworkCore/) [![Serilog.Exceptions.EntityFrameworkCore package in serilog-exceptions feed in Azure Artifacts](https://feeds.dev.azure.com/serilog-exceptions/_apis/public/Packaging/Feeds/8479813c-da6b-4677-b40d-78df8725dc9c/Packages/ee2cd6f8-4c93-4774-9398-23c49ba41928/Badge)](https://dev.azure.com/serilog-exceptions/Serilog.Exceptions/_packaging?_a=package&feed=8479813c-da6b-4677-b40d-78df8725dc9c&package=ee2cd6f8-4c93-4774-9398-23c49ba41928&preferRelease=true) diff --git a/Serilog.Exceptions.sln b/Serilog.Exceptions.sln index 6ff4d608..10c5da12 100644 --- a/Serilog.Exceptions.sln +++ b/Serilog.Exceptions.sln @@ -88,6 +88,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ .github\workflows\release-drafter.yml = .github\workflows\release-drafter.yml EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Exceptions.MsSqlServer", "Source\Serilog.Exceptions.MsSqlServer\Serilog.Exceptions.MsSqlServer.csproj", "{0A21D2AD-024B-4F3D-95F4-BAEFEEE95945}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -118,6 +120,10 @@ Global {B6603A53-F254-43ED-9A03-08D95F8F3BAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {B6603A53-F254-43ED-9A03-08D95F8F3BAC}.Release|Any CPU.ActiveCfg = Release|Any CPU {B6603A53-F254-43ED-9A03-08D95F8F3BAC}.Release|Any CPU.Build.0 = Release|Any CPU + {0A21D2AD-024B-4F3D-95F4-BAEFEEE95945}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0A21D2AD-024B-4F3D-95F4-BAEFEEE95945}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0A21D2AD-024B-4F3D-95F4-BAEFEEE95945}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0A21D2AD-024B-4F3D-95F4-BAEFEEE95945}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -133,6 +139,7 @@ Global {C50E9892-B2DF-43A6-9E00-BA2952933FA4} = {D2CAFFAF-CBF6-493A-AA61-4916FE7D586C} {2C245036-D7F6-4F7C-9BB6-5AFBCCE480F7} = {76FBEEA2-0F88-487E-99C3-5D865CBE79B6} {4F089B23-3121-4935-B24E-7A9A497BD9FE} = {2C245036-D7F6-4F7C-9BB6-5AFBCCE480F7} + {0A21D2AD-024B-4F3D-95F4-BAEFEEE95945} = {C5508012-7216-4ABE-AB2F-B166ED5FF94F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BE74AFAC-AC6F-4B80-860F-15C22BEE1A38} diff --git a/Source/Serilog.Exceptions.MsSqlServer/Destructurers/SqlExceptionDestructurer.cs b/Source/Serilog.Exceptions.MsSqlServer/Destructurers/SqlExceptionDestructurer.cs new file mode 100644 index 00000000..18ec990b --- /dev/null +++ b/Source/Serilog.Exceptions.MsSqlServer/Destructurers/SqlExceptionDestructurer.cs @@ -0,0 +1,39 @@ +namespace Serilog.Exceptions.MsSqlServer.Destructurers +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Microsoft.Data.SqlClient; + using Serilog.Exceptions.Core; + using Serilog.Exceptions.Destructurers; + + /// + /// A destructurer for . + /// + /// + public class SqlExceptionDestructurer : ExceptionDestructurer + { + /// + public override Type[] TargetTypes => new[] { typeof(SqlException) }; + + /// + public override void Destructure( + Exception exception, + IExceptionPropertiesBag propertiesBag, + Func> destructureException) + { + base.Destructure(exception, propertiesBag, destructureException); + +#pragma warning disable CA1062 // Validate arguments of public methods + var sqlException = (SqlException)exception; + propertiesBag.AddProperty(nameof(SqlException.ClientConnectionId), sqlException.ClientConnectionId); + propertiesBag.AddProperty(nameof(SqlException.Class), sqlException.Class); + propertiesBag.AddProperty(nameof(SqlException.LineNumber), sqlException.LineNumber); + propertiesBag.AddProperty(nameof(SqlException.Number), sqlException.Number); + propertiesBag.AddProperty(nameof(SqlException.Server), sqlException.Server); + propertiesBag.AddProperty(nameof(SqlException.State), sqlException.State); + propertiesBag.AddProperty(nameof(SqlException.Errors), sqlException.Errors.Cast().ToArray()); +#pragma warning restore CA1062 // Validate arguments of public methods + } + } +} diff --git a/Source/Serilog.Exceptions.MsSqlServer/Properties/AssemblyInfo.cs b/Source/Serilog.Exceptions.MsSqlServer/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..fb51532b --- /dev/null +++ b/Source/Serilog.Exceptions.MsSqlServer/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Serilog.Exceptions.Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001007320f17cde01a37b8b3dff91961943f76893c63352544608de15974586e17739e0b232cdc8cc8762b40d988a21522024abca8116605ed1c8958f350bc6ca4594aa1ca61c1906d85ee76df8d9b404ffd65a5f6efeddd486bddd5b74ff9a7449af0dc9d43da753a3023b20ca616e8313e82a18719b6551f37c45c8d8e4e4cf9bac")] \ No newline at end of file diff --git a/Source/Serilog.Exceptions.MsSqlServer/Serilog.Exceptions.MsSqlServer.csproj b/Source/Serilog.Exceptions.MsSqlServer/Serilog.Exceptions.MsSqlServer.csproj new file mode 100644 index 00000000..44e2526d --- /dev/null +++ b/Source/Serilog.Exceptions.MsSqlServer/Serilog.Exceptions.MsSqlServer.csproj @@ -0,0 +1,22 @@ + + + + netstandard2.0 + + + + Serilog Exceptions + Log exception details and custom properties that are not output in Exception.ToString(). Contains custom destructurers for SQL Server exceptions. + Serilog;Exception;Log;Logging;Detail;Details;SQL;Server + + + + + + + + + + + + diff --git a/Tests/Serilog.Exceptions.Test/Serilog.Exceptions.Test.csproj b/Tests/Serilog.Exceptions.Test/Serilog.Exceptions.Test.csproj index abed85c0..404b2ab4 100644 --- a/Tests/Serilog.Exceptions.Test/Serilog.Exceptions.Test.csproj +++ b/Tests/Serilog.Exceptions.Test/Serilog.Exceptions.Test.csproj @@ -11,7 +11,6 @@ -