1313 /// </summary>
1414 public class OperationResult
1515 {
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 ( ) ;
20+
21+ private readonly List < string > _successMessages = new ( ) ;
1722
18- protected readonly ILogger ? _logger ;
23+ private readonly ILogger ? _logger ;
1924
2025 /// <summary>
2126 /// Gets or sets a value indicating whether the operation is successful or not.
@@ -44,7 +49,7 @@ public IEnumerable<string>? SuccessMessages
4449 /// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />.
4550 /// </summary>
4651 public List < IOperationError > Errors { get ; internal set ; } = new List < IOperationError > ( ) ;
47-
52+
4853 /// <summary>
4954 /// Gets or sets the first exception that resulted from the operation.
5055 /// </summary>
@@ -93,18 +98,22 @@ public OperationResult AppendErrors(OperationResult otherOperationResult)
9398 {
9499 if ( otherOperationResult is null ) return this ;
95100
96- foreach ( var error in otherOperationResult . Errors )
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
97107 {
98- this . AppendErrorInternal ( error ) ;
99-
100- // Logs messages if the other operation result does not have a logger
101- if ( this . _logger is not null && otherOperationResult . _logger is null && ! error . Logged )
102- {
103- this . _logger . Log ( GetLogLevel ( error . LogLevel ) , error . Message ) ;
104- error . Logged = true ;
105- }
108+ foreach ( var ( error , logLevel ) in otherOperationResult . _errorsNotLogged )
109+ this . LogInternal ( error , logLevel ) ;
110+
111+ otherOperationResult . _errorsNotLogged . Clear ( ) ;
106112 }
107113
114+ // Append the error message without logging (presuming that there is already a log message).
115+ foreach ( var error in otherOperationResult . Errors ) this . AppendErrorInternal ( error ) ;
116+
108117 return this ;
109118 }
110119
@@ -120,7 +129,7 @@ public OperationResult AppendError(string message, int? code = null, LogLevel? l
120129 {
121130 if ( string . IsNullOrWhiteSpace ( message ) ) throw new ArgumentNullException ( nameof ( message ) ) ;
122131
123- var error = new OperationError ( message , code ) { Details = details , LogLevel = logLevel } ;
132+ var error = new OperationError ( message , code ) { Details = details } ;
124133 this . AppendError ( error , logLevel ) ;
125134
126135 return this ;
@@ -140,7 +149,7 @@ public OperationResult AppendError<T>(string message, int? code = null, LogLevel
140149 {
141150 if ( string . IsNullOrWhiteSpace ( message ) ) throw new ArgumentNullException ( nameof ( message ) ) ;
142151
143- var error = new T ( ) { Message = message , Code = code , Details = details , LogLevel = logLevel } ;
152+ var error = new T ( ) { Message = message , Code = code , Details = details } ;
144153 this . AppendError ( error , logLevel ) ;
145154
146155 return this ;
@@ -155,31 +164,26 @@ public OperationResult AppendError<T>(string message, int? code = null, LogLevel
155164 public OperationResult AppendError ( IOperationError error , LogLevel ? logLevel = LogLevel . Error )
156165 {
157166 this . AppendErrorInternal ( error ) ;
158-
159- if ( this . _logger != null )
160- {
161- this . _logger . Log ( GetLogLevel ( logLevel ) , error . Message ) ;
162- error . Logged = true ;
163- }
167+ this . LogInternal ( error , logLevel ) ;
164168
165169 return this ;
166170 }
167171
168172 /// <summary>
169- /// 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.
170174 /// </summary>
171175 /// <param name="exception">The exception to log.</param>
172176 /// <param name="errorCode">The error code.</param>
173- /// <param name="logLevel">The <see cref="LogEventLevel "/> logging severity.</param>
177+ /// <param name="logLevel">The <see cref="LogLevel "/> logging severity.</param>
174178 /// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
175- public OperationResult AppendException ( Exception exception , int ? errorCode = null , LogLevel ? logLevel = null )
179+ public OperationResult AppendException ( Exception exception , int errorCode = 0 , LogLevel ? logLevel = null )
176180 {
177181 if ( exception is null ) throw new ArgumentNullException ( nameof ( exception ) ) ;
178182
179183 // Append the exception as a first if it is not yet set.
180184 this . InitialException ??= exception ;
181185
182- var error = new OperationError ( exception . ToString ( ) , errorCode , null , LogLevel . Error ) ;
186+ var error = new OperationError ( exception . ToString ( ) , errorCode ) ;
183187 this . AppendError ( error , logLevel ) ;
184188
185189 return this ;
@@ -218,13 +222,28 @@ public static OperationResult FromError(string message, int? code = null, LogLev
218222 return result . AppendError ( message , code , logLevel , details ) ;
219223 }
220224
221- protected static LogLevel GetLogLevel ( LogLevel ? optionalLevel ) => optionalLevel ?? LogLevel . Error ;
222-
223225 /// <summary>
224226 /// Appends an <see cref="IOperationError"/> to the internal errors collection.
225227 /// </summary>
226228 /// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
227229 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+ }
228247 }
229248
230249 /// <summary>
@@ -291,41 +310,14 @@ public OperationResult(TResult resultObject) : base()
291310 return this ;
292311 }
293312
294- /// <summary>
295- /// Appends error messages from <paramref name="otherOperationResult"/> to the current instance.
296- /// </summary>
297- /// <param name="otherOperationResult">The <see cref="OperationResult"/> to append from.</param>
298- /// <typeparam name="TOther">A type that inherits from <see cref="OperationResult"/>.</typeparam>
299- /// <returns>The original <see cref="OperationResult"/> with the appended messages from <paramref name="otherOperationResult"/>.</returns>
300- [ Obsolete ( "Please use AppendErrors instead. This method will be removed to avoid confusion." ) ]
301- public OperationResult < TResult > AppendErrorMessages < TOther > ( TOther otherOperationResult )
302- where TOther : OperationResult
303- {
304- base . AppendErrors ( otherOperationResult ) ;
305-
306- return this ;
307- }
308-
309- /// <summary>
310- /// Appends error from <paramref name="otherOperationResult"/> to the current instance.
311- /// </summary>
312- /// <param name="otherOperationResult">The <see cref="OperationResult"/> to append from.</param>
313- /// <returns>The original <see cref="OperationResult"/> with the appended messages from <paramref name="otherOperationResult"/>.</returns>
314- public new OperationResult < TResult > AppendErrors ( OperationResult otherOperationResult )
315- {
316- base . AppendErrors ( otherOperationResult ) ;
317-
318- return this ;
319- }
320-
321313 /// <summary>
322314 /// 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.
323315 /// </summary>
324316 /// <param name="exception">The exception to log.</param>
325317 /// <param name="errorCode">The error code.</param>
326318 /// <param name="logLevel">The <see cref="LogEventLevel"/> logging severity.</param>
327319 /// <returns>The current instance of the <see cref="OperationResult{TResult}"/>.</returns>
328- 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 )
329321 {
330322 base . AppendException ( exception , errorCode , logLevel ) ;
331323
0 commit comments