-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78012bb
commit 4b306d5
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Source/Serilog.Exceptions.Grpc/Destructurers/RpcExceptionDestructurer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Serilog.Exceptions.Grpc.Destructurers; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using global::Grpc.Core; | ||
using Serilog.Exceptions.Core; | ||
using Serilog.Exceptions.Destructurers; | ||
|
||
/// <summary> | ||
/// A destructurer for <see cref="RpcException"/>. | ||
/// </summary> | ||
/// <seealso cref="ExceptionDestructurer" /> | ||
public class RpcExceptionDestructurer : ExceptionDestructurer | ||
{ | ||
/// <inheritdoc /> | ||
public override Type[] TargetTypes => new[] { typeof(RpcException) }; | ||
|
||
/// <inheritdoc /> | ||
public override void Destructure( | ||
Exception exception, | ||
IExceptionPropertiesBag propertiesBag, | ||
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException) | ||
{ | ||
base.Destructure(exception, propertiesBag, destructureException); | ||
|
||
var rpcException = (RpcException)exception; | ||
|
||
#pragma warning disable CA1062 // Validate arguments of public methods | ||
propertiesBag.AddProperty(nameof(RpcException.Status.StatusCode), rpcException.Status.StatusCode); | ||
propertiesBag.AddProperty(nameof(RpcException.Status.Detail), rpcException.Status.Detail); | ||
|
||
foreach (var trailer in rpcException.Trailers) | ||
{ | ||
if (trailer.IsBinary) | ||
{ | ||
continue; | ||
} | ||
|
||
propertiesBag.AddProperty($"{nameof(RpcException.Trailers)}.{trailer.Key}", trailer.Value); | ||
} | ||
#pragma warning restore CA1062 // Validate arguments of public methods | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System; | ||
|
||
[assembly: CLSCompliant(true)] |
21 changes: 21 additions & 0 deletions
21
Source/Serilog.Exceptions.Grpc/Serilog.Exceptions.Grpc.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup Label="Build"> | ||
<TargetFrameworks>netstandard2.1;netstandard2.0;netstandard1.5;net462</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="Package"> | ||
<Product>Serilog Exceptions gRPC</Product> | ||
<Description>Log exception details and custom properties that are not output in Exception.ToString(). Contains custom destructurers for gRPC exceptions.</Description> | ||
<PackageTags>Serilog;Exception;Log;Logging;Detail;Details;gRPC</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Label="Project References"> | ||
<ProjectReference Include="..\Serilog.Exceptions\Serilog.Exceptions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Label="Package References"> | ||
<PackageReference Include="Grpc.Core.Api" Version="2.47.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
59 changes: 59 additions & 0 deletions
59
Tests/Serilog.Exceptions.Test/Destructurers/RpcExceptionDestructurerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace Serilog.Exceptions.Test.Destructurers; | ||
|
||
using System; | ||
using System.Net; | ||
using global::Grpc.Core; | ||
using Serilog.Exceptions.Core; | ||
using Serilog.Exceptions.Grpc.Destructurers; | ||
using Xunit; | ||
using static LogJsonOutputUtils; | ||
|
||
public class RpcExceptionDestructurerTest | ||
{ | ||
[Fact] | ||
public void RpcException_StatusCodeIsLoggedAsProperty() | ||
{ | ||
var options = new DestructuringOptionsBuilder().WithDestructurers(new[] { new RpcExceptionDestructurer() }); | ||
var rpcException = new RpcException(new Status(StatusCode.Aborted, string.Empty)); | ||
|
||
Test_LoggedExceptionContainsProperty(rpcException, nameof(RpcException.Status.StatusCode), nameof(StatusCode.Aborted), options); | ||
} | ||
|
||
[Fact] | ||
public void RpcException_StatusDetailIsLoggedAsProperty() | ||
{ | ||
var options = new DestructuringOptionsBuilder().WithDestructurers(new[] { new RpcExceptionDestructurer() }); | ||
var testDetail = "details"; | ||
var rpcException = new RpcException(new Status(StatusCode.Aborted, testDetail)); | ||
|
||
Test_LoggedExceptionContainsProperty(rpcException, nameof(RpcException.Status.Detail), testDetail, options); | ||
} | ||
|
||
[Fact] | ||
public void RpcException_TrailersAreLoggedAsProperty() | ||
{ | ||
var options = new DestructuringOptionsBuilder().WithDestructurers(new[] { new RpcExceptionDestructurer() }); | ||
const string stringTrailerKey1 = "key1"; | ||
const string stringTrailerValue1 = "stringTrailerValue1"; | ||
const string stringTrailerKey2 = "key2"; | ||
const string stringTrailerValue2 = "stringTrailerValue2"; | ||
var metadata = new Metadata { { stringTrailerKey1, stringTrailerValue1 }, { stringTrailerKey2, stringTrailerValue2 } }; | ||
|
||
var rpcException = new RpcException(new Status(StatusCode.Aborted, string.Empty), metadata); | ||
|
||
Test_LoggedExceptionContainsProperty(rpcException, $"{nameof(RpcException.Trailers)}.{stringTrailerKey1}", stringTrailerValue1, options); | ||
Test_LoggedExceptionContainsProperty(rpcException, $"{nameof(RpcException.Trailers)}.{stringTrailerKey2}", stringTrailerValue2, options); | ||
} | ||
|
||
[Fact] | ||
public void RpcException_BinaryTrailersAreNotLoggedAsProperty() | ||
{ | ||
var options = new DestructuringOptionsBuilder().WithDestructurers(new[] { new RpcExceptionDestructurer() }); | ||
const string stringTrailerKey1 = "key-bin"; | ||
var metadata = new Metadata { { stringTrailerKey1, new byte[] { 1 } } }; | ||
|
||
var rpcException = new RpcException(new Status(StatusCode.Aborted, string.Empty), metadata); | ||
|
||
Test_LoggedExceptionDoesNotContainProperty(rpcException, $"{nameof(RpcException.Trailers)}.{stringTrailerKey1}", options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters