| | 1 | | using Kestrun.Logging.Exceptions; |
| | 2 | | using Serilog.Core; |
| | 3 | | using Serilog.Events; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Logging.Enrichers; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Enriches Serilog log events with error record and invocation info from WrapperException. |
| | 9 | | /// </summary> |
| | 10 | | public class ErrorRecordEnricher : ILogEventEnricher |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The property name used for the error record in log events. |
| | 14 | | /// </summary> |
| | 15 | | public const string ERR_PROPERTY_NAME_FULL = "ErrorRecord"; |
| | 16 | | /// <summary> |
| | 17 | | /// The property name used for the invocation info in log events. |
| | 18 | | /// </summary> |
| | 19 | | public const string II_PROPERTY_NAME_FULL = "InvocationInfo"; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Gets a value indicating whether objects should be destructured when enriching log events. |
| | 23 | | /// </summary> |
| 4 | 24 | | public bool DestructureObjects { get; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="ErrorRecordEnricher"/> class. |
| | 28 | | /// </summary> |
| 0 | 29 | | public ErrorRecordEnricher() |
| | 30 | | { |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="ErrorRecordEnricher"/> class with the option to destructure objects |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="destructureObjects">Indicates whether objects should be destructured when enriching log events.</pa |
| 6 | 37 | | public ErrorRecordEnricher(bool destructureObjects) => DestructureObjects = destructureObjects; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Enriches the log event with error record and invocation info properties if the exception is a <see cref="Wrapper |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="logEvent">The log event to enrich.</param> |
| | 43 | | /// <param name="propertyFactory">The property factory used to create log event properties.</param> |
| | 44 | | public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) |
| | 45 | | { |
| 3 | 46 | | if (logEvent.Exception is WrapperException wrapperException) |
| | 47 | | { |
| 2 | 48 | | logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(ERR_PROPERTY_NAME_FULL, wrapperException.ErrorRe |
| 2 | 49 | | logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty( |
| 2 | 50 | | II_PROPERTY_NAME_FULL, |
| 2 | 51 | | wrapperException.ErrorRecordWrapper?.InvocationInfoWrapper, |
| 2 | 52 | | DestructureObjects)); |
| | 53 | | } |
| 3 | 54 | | } |
| | 55 | | } |