Skip to content

Commit 77845ac

Browse files
fixing log counts
1 parent 6620663 commit 77845ac

5 files changed

Lines changed: 42 additions & 28 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
namespace OneBitSoftware.Utilities.Errors
22
{
3+
using Microsoft.Extensions.Logging;
4+
35
public interface IOperationError
46
{
57
int? Code { get; set; }
68

79
string? Message { get; set; }
810

911
string? Details { get; set; }
12+
13+
/// <summary>
14+
/// Defines if the error is logged or not. Used when merging <cref="OperationResult"/> instances and one of them does not have an ILogger.
15+
/// </summary>
16+
bool Logged { get; internal set; }
17+
18+
/// <summary>
19+
/// Defines the log level for the error.
20+
/// </summary>
21+
LogLevel? LogLevel { get; set; }
1022
}
1123
}

src/OneBitSoftware.Utilities.OperationResult/Errors/OperationError.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace OneBitSoftware.Utilities.Errors
22
{
33
using System.Text;
4+
using Microsoft.Extensions.Logging;
45

56
public class OperationError : IOperationError
67
{
@@ -17,12 +18,20 @@ public OperationError(string? message = null, int? code = null, string? details
1718

1819
public string? Details { get; set; }
1920

21+
/// <inheritdoc />
22+
public LogLevel? LogLevel { get; set; }
23+
24+
/// <inheritdoc />
25+
bool IOperationError.Logged { get; set; }
26+
2027
public override string ToString()
2128
{
2229
var result = new StringBuilder();
2330

2431
if (this.Code != null) result.AppendLine($"Code: {this.Code}");
2532

33+
if (this.LogLevel is not null) result.AppendLine($"Severity: {this.LogLevel}"); // TODO: maybe convert to string?
34+
2635
if (!string.IsNullOrWhiteSpace(this.Message)) result.AppendLine($"Message: {this.Message}");
2736

2837
if (!string.IsNullOrWhiteSpace(this.Details)) result.AppendLine($"Trace: {this.Details}");

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,10 @@ public OperationResult AppendErrors(OperationResult otherOperationResult)
9898
this.AppendErrorInternal(error);
9999

100100
// Logs messages if the other operation result does not have a logger
101-
// BUG: We don't know the severity of the message and need to assume a level.
102-
// The task is to make IOperationError have info about the severity, so it can be used here
103-
if (this._logger is not null && otherOperationResult._logger is null)
101+
if (this._logger is not null && otherOperationResult._logger is null && !error.Logged)
104102
{
105-
this._logger.Log(LogLevel.Error, error.Message);
103+
this._logger.Log(GetLogLevel(error.LogLevel), error.Message);
104+
error.Logged = true;
106105
}
107106
}
108107

@@ -159,9 +158,8 @@ public OperationResult AppendError(IOperationError error, LogLevel? logLevel = L
159158

160159
if (this._logger != null)
161160
{
162-
#pragma warning disable CA2254 // Template should be a static expression
163161
this._logger.Log(GetLogLevel(logLevel), error.Message);
164-
#pragma warning restore CA2254 // Template should be a static expression
162+
error.Logged = true;
165163
}
166164

167165
return this;
@@ -220,7 +218,6 @@ public static OperationResult FromError(string message, int? code = null, LogLev
220218
return result.AppendError(message, code, logLevel, details);
221219
}
222220

223-
// TODO: this method needs completing.
224221
protected static LogLevel GetLogLevel(LogLevel? optionalLevel) => optionalLevel ?? LogLevel.Error;
225222

226223
/// <summary>
@@ -294,26 +291,6 @@ public OperationResult(TResult resultObject) : base()
294291
return this;
295292
}
296293

297-
/// <summary>
298-
/// Appends an <see cref="IOperationError"/> to the internal errors collection.
299-
/// </summary>
300-
/// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
301-
/// <param name="logLevel">The logging level.</param>
302-
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
303-
public new OperationResult<TResult> AppendError(IOperationError error, LogLevel? logLevel = LogLevel.Error)
304-
{
305-
base.AppendErrorInternal(error);
306-
307-
if (this._logger != null)
308-
{
309-
#pragma warning disable CA2254 // Template should be a static expression
310-
this._logger.Log(GetLogLevel(logLevel), error.Message);
311-
#pragma warning restore CA2254 // Template should be a static expression
312-
}
313-
314-
return this;
315-
}
316-
317294
/// <summary>
318295
/// Appends error messages from <paramref name="otherOperationResult"/> to the current instance.
319296
/// </summary>

src/OneBitSoftware.Utilities.OperationResult/PolymorphicOperationErrorSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
5454
if (this._typeMappings.TryGetValue(value.GetType(), out var typeValue) == false) throw new InvalidOperationException($"Model of type {value.GetType()} cannot be successfully serialized.");
5555

5656
var tempBufferWriter = new ArrayBufferWriter<byte>();
57-
var tempWriter = new Utf8JsonWriter(tempBufferWriter);
57+
var tempWriter = new Utf8JsonWriter(tempBufferWriter); // TODO: dispose with using var
5858

5959
var fallbackDeserializationOptions = this.ConstructSafeFallbackOptions(options);
6060
JsonSerializer.Serialize(tempWriter, value, value.GetType(), fallbackDeserializationOptions);

tests/OneBitSoftware.Utilities.OperationResultTests/OperationResultAppendErrorsTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,23 @@ public void AppendErrors_ShouldLogOnceWhenCreatedWithALogger()
144144
Assert.Equal(1, testLogger.LogMessages.Count);
145145
}
146146

147+
[Fact]
148+
public void AppendErrors_ShouldLogOnceWhenNestingWithALogger()
149+
{
150+
// Arrange
151+
var testLogger = new TestLogger();
152+
var operationResultWithLogger = new OperationResult(testLogger);
153+
var operationResultWithLogger2 = new OperationResult(testLogger);
154+
var operationResultWithLogger3 = new OperationResult(testLogger);
147155

156+
// Act
157+
operationResultWithLogger3.AppendError("test1");
158+
operationResultWithLogger2.AppendError("test2");
159+
operationResultWithLogger.AppendErrors(operationResultWithLogger2);
160+
161+
// Assert
162+
Assert.Equal(2, testLogger.LogMessages.Count);
163+
}
148164

149165
[Fact]
150166
public void AppendErrors_ShouldLogWhenCreatedWithNoLogger()

0 commit comments

Comments
 (0)