|
13 | 13 | /// </summary> |
14 | 14 | public class OperationResult |
15 | 15 | { |
16 | | - private readonly List<string> _successMessages = new List<string>(); |
| 16 | + /// <summary> |
| 17 | + /// Contains <see cref="IOperationError"/> instances that have not been logged. |
| 18 | + /// </summary> |
| 19 | + private readonly List<(IOperationError Error, LogLevel? LogLevel)> _errorsNotLogged = new(); |
17 | 20 |
|
18 | | - protected readonly ILogger? _logger; |
| 21 | + private readonly List<string> _successMessages = new(); |
| 22 | + |
| 23 | + private readonly ILogger? _logger; |
19 | 24 |
|
20 | 25 | /// <summary> |
21 | 26 | /// Gets or sets a value indicating whether the operation is successful or not. |
@@ -44,7 +49,7 @@ public IEnumerable<string>? SuccessMessages |
44 | 49 | /// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />. |
45 | 50 | /// </summary> |
46 | 51 | public List<IOperationError> Errors { get; internal set; } = new List<IOperationError>(); |
47 | | - |
| 52 | + |
48 | 53 | /// <summary> |
49 | 54 | /// Gets or sets the first exception that resulted from the operation. |
50 | 55 | /// </summary> |
@@ -93,6 +98,19 @@ public OperationResult AppendErrors(OperationResult otherOperationResult) |
93 | 98 | { |
94 | 99 | if (otherOperationResult is null) return this; |
95 | 100 |
|
| 101 | + // store any messages for logging at a later stage, when merged to an OperationResult with a logger. |
| 102 | + if (this._logger is null) |
| 103 | + { |
| 104 | + this._errorsNotLogged.AddRange(otherOperationResult._errorsNotLogged); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + foreach (var (error, logLevel) in otherOperationResult._errorsNotLogged) |
| 109 | + this.LogInternal(error, logLevel); |
| 110 | + |
| 111 | + otherOperationResult._errorsNotLogged.Clear(); |
| 112 | + } |
| 113 | + |
96 | 114 | // Append the error message without logging (presuming that there is already a log message). |
97 | 115 | foreach (var error in otherOperationResult.Errors) this.AppendErrorInternal(error); |
98 | 116 |
|
@@ -146,25 +164,19 @@ public OperationResult AppendError<T>(string message, int? code = null, LogLevel |
146 | 164 | public OperationResult AppendError(IOperationError error, LogLevel? logLevel = LogLevel.Error) |
147 | 165 | { |
148 | 166 | this.AppendErrorInternal(error); |
149 | | - |
150 | | - if (this._logger != null) |
151 | | - { |
152 | | -#pragma warning disable CA2254 // Template should be a static expression |
153 | | - this._logger.Log(GetLogLevel(logLevel), error.Message); |
154 | | -#pragma warning restore CA2254 // Template should be a static expression |
155 | | - } |
| 167 | + this.LogInternal(error, logLevel); |
156 | 168 |
|
157 | 169 | return this; |
158 | 170 | } |
159 | 171 |
|
160 | 172 | /// <summary> |
161 | | - /// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogEventLevel"/> level. A call to this method will set the Success property to false. |
| 173 | + /// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogLevel"/> level. A call to this method will set the Success property to false. |
162 | 174 | /// </summary> |
163 | 175 | /// <param name="exception">The exception to log.</param> |
164 | 176 | /// <param name="errorCode">The error code.</param> |
165 | | - /// <param name="logLevel">The <see cref="LogEventLevel"/> logging severity.</param> |
| 177 | + /// <param name="logLevel">The <see cref="LogLevel"/> logging severity.</param> |
166 | 178 | /// <returns>The current instance of the <see cref="OperationResult"/>.</returns> |
167 | | - public OperationResult AppendException(Exception exception, int? errorCode = null, LogLevel? logLevel = null) |
| 179 | + public OperationResult AppendException(Exception exception, int errorCode = 0, LogLevel? logLevel = null) |
168 | 180 | { |
169 | 181 | if (exception is null) throw new ArgumentNullException(nameof(exception)); |
170 | 182 |
|
@@ -210,14 +222,28 @@ public static OperationResult FromError(string message, int? code = null, LogLev |
210 | 222 | return result.AppendError(message, code, logLevel, details); |
211 | 223 | } |
212 | 224 |
|
213 | | - // TODO: this method needs completing. |
214 | | - protected static LogLevel GetLogLevel(LogLevel? optionalLevel) => optionalLevel ?? LogLevel.Error; |
215 | | - |
216 | 225 | /// <summary> |
217 | 226 | /// Appends an <see cref="IOperationError"/> to the internal errors collection. |
218 | 227 | /// </summary> |
219 | 228 | /// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param> |
220 | 229 | protected void AppendErrorInternal(IOperationError error) => this.Errors.Add(error); |
| 230 | + |
| 231 | + /// <summary> |
| 232 | + /// Logs to the internal logger if it is set, otherwise it will add the error to the internal errors collection. |
| 233 | + /// </summary> |
| 234 | + /// <param name="error">The <see cref="IOperationError"/> to log.</param> |
| 235 | + /// <param name="logLevel">The log level.</param> |
| 236 | + private void LogInternal(IOperationError error, LogLevel? logLevel) |
| 237 | + { |
| 238 | + if (this._logger is null) |
| 239 | + { |
| 240 | + this._errorsNotLogged.Add((Error: error, LogLevel: logLevel)); |
| 241 | + } |
| 242 | + else |
| 243 | + { |
| 244 | + this._logger.Log(logLevel ?? LogLevel.Error, error.Message); |
| 245 | + } |
| 246 | + } |
221 | 247 | } |
222 | 248 |
|
223 | 249 | /// <summary> |
@@ -284,61 +310,14 @@ public OperationResult(TResult resultObject) : base() |
284 | 310 | return this; |
285 | 311 | } |
286 | 312 |
|
287 | | - /// <summary> |
288 | | - /// Appends an <see cref="IOperationError"/> to the internal errors collection. |
289 | | - /// </summary> |
290 | | - /// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param> |
291 | | - /// <param name="logLevel">The logging level.</param> |
292 | | - /// <returns>The current instance of the <see cref="OperationResult"/>.</returns> |
293 | | - public new OperationResult<TResult> AppendError(IOperationError error, LogLevel? logLevel = LogLevel.Error) |
294 | | - { |
295 | | - base.AppendErrorInternal(error); |
296 | | - |
297 | | - if (this._logger != null) |
298 | | - { |
299 | | -#pragma warning disable CA2254 // Template should be a static expression |
300 | | - this._logger.Log(GetLogLevel(logLevel), error.Message); |
301 | | -#pragma warning restore CA2254 // Template should be a static expression |
302 | | - } |
303 | | - |
304 | | - return this; |
305 | | - } |
306 | | - |
307 | | - /// <summary> |
308 | | - /// Appends error messages from <paramref name="otherOperationResult"/> to the current instance. |
309 | | - /// </summary> |
310 | | - /// <param name="otherOperationResult">The <see cref="OperationResult"/> to append from.</param> |
311 | | - /// <typeparam name="TOther">A type that inherits from <see cref="OperationResult"/>.</typeparam> |
312 | | - /// <returns>The original <see cref="OperationResult"/> with the appended messages from <paramref name="otherOperationResult"/>.</returns> |
313 | | - [Obsolete("Please use AppendErrors instead. This method will be removed to avoid confusion.")] |
314 | | - public OperationResult<TResult> AppendErrorMessages<TOther>(TOther otherOperationResult) |
315 | | - where TOther : OperationResult |
316 | | - { |
317 | | - base.AppendErrors(otherOperationResult); |
318 | | - |
319 | | - return this; |
320 | | - } |
321 | | - |
322 | | - /// <summary> |
323 | | - /// Appends error from <paramref name="otherOperationResult"/> to the current instance. |
324 | | - /// </summary> |
325 | | - /// <param name="otherOperationResult">The <see cref="OperationResult"/> to append from.</param> |
326 | | - /// <returns>The original <see cref="OperationResult"/> with the appended messages from <paramref name="otherOperationResult"/>.</returns> |
327 | | - public new OperationResult<TResult> AppendErrors(OperationResult otherOperationResult) |
328 | | - { |
329 | | - base.AppendErrors(otherOperationResult); |
330 | | - |
331 | | - return this; |
332 | | - } |
333 | | - |
334 | 313 | /// <summary> |
335 | 314 | /// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogEventLevel"/> level. A call to this method will set the Success property to false. |
336 | 315 | /// </summary> |
337 | 316 | /// <param name="exception">The exception to log.</param> |
338 | 317 | /// <param name="errorCode">The error code.</param> |
339 | 318 | /// <param name="logLevel">The <see cref="LogEventLevel"/> logging severity.</param> |
340 | 319 | /// <returns>The current instance of the <see cref="OperationResult{TResult}"/>.</returns> |
341 | | - public new OperationResult<TResult> AppendException(Exception exception, int? errorCode = null, LogLevel? logLevel = null) |
| 320 | + public new OperationResult<TResult> AppendException(Exception exception, int errorCode = 0, LogLevel? logLevel = null) |
342 | 321 | { |
343 | 322 | base.AppendException(exception, errorCode, logLevel); |
344 | 323 |
|
|
0 commit comments