You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -38,6 +39,28 @@ public void AddOrUpdate(LogEventProperty property)
38
39
}
39
40
}
40
41
42
+
/// <summary>
43
+
/// Set an exception to include in the Serilog request log event.
44
+
/// </summary>
45
+
/// <example>
46
+
/// Passing an exception to the diagnostic context is useful when unhandled exceptions are handled before reaching Serilog's
47
+
/// RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform
48
+
/// exceptions to ProblemDetails responses.
49
+
/// </example>
50
+
/// <remarks>
51
+
/// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception takes precedence.<br/>
52
+
/// If <c>null</c> is given, it clears any previously assigned exception.
53
+
/// </remarks>
54
+
/// <param name="exception">The exception to log.</param>
55
+
publicvoidSetException(Exceptionexception)
56
+
{
57
+
lock(_propertiesLock)
58
+
{
59
+
if(_properties==null)return;
60
+
_exception=exception;
61
+
}
62
+
}
63
+
41
64
/// <summary>
42
65
/// Complete the context and retrieve the properties added to it, if any. This will
43
66
/// stop collection and remove the collector from the original execution context and
@@ -51,6 +74,36 @@ public bool TryComplete(out IEnumerable<LogEventProperty> properties)
51
74
{
52
75
properties=_properties?.Values;
53
76
_properties=null;
77
+
_exception=null;
78
+
Dispose();
79
+
returnproperties!=null;
80
+
}
81
+
}
82
+
83
+
/// <summary>
84
+
/// Complete the context and retrieve the properties added to it, if any. This will
85
+
/// stop collection and remove the collector from the original execution context and
86
+
/// any of its children.
87
+
/// </summary>
88
+
/// <param name="properties">The collected properties, or null if no collection is active.</param>
89
+
/// <param name="exception">The collected exception, or null if none has been collected or if no collection is active.</param>
90
+
/// <returns>True if properties could be collected.</returns>
91
+
/// <example>
92
+
/// This overload provides the exception collected by the diagnostic context, which is useful when unhandled exceptions are handled
93
+
/// before reaching Serilog's RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform
94
+
/// exceptions to ProblemDetails responses.
95
+
/// </example>
96
+
/// <remarks>
97
+
/// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception should take precedence.<br/>
0 commit comments