-
-
Notifications
You must be signed in to change notification settings - Fork 108
Enhance DefaultLogger with extensibility features #4131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,11 @@ public class DefaultLogger(Context context) : TUnitLogger | |
| { | ||
| private readonly ConcurrentDictionary<string, List<string>> _values = new(); | ||
|
|
||
| /// <summary> | ||
| /// Gets the context associated with this logger. | ||
| /// </summary> | ||
| protected Context Context => context; | ||
|
|
||
| public void PushProperties(IDictionary<string, List<object>> dictionary) | ||
| { | ||
| foreach (var keyValuePair in dictionary) | ||
|
|
@@ -52,16 +57,7 @@ public override async ValueTask LogAsync<TState>(LogLevel logLevel, TState state | |
|
|
||
| var message = GenerateMessage(formatter(state, exception), exception, logLevel); | ||
|
|
||
| if (logLevel >= LogLevel.Error) | ||
| { | ||
| await context.ErrorOutputWriter.WriteLineAsync(message); | ||
| await GlobalContext.Current.OriginalConsoleError.WriteLineAsync(message); | ||
| } | ||
| else | ||
| { | ||
| await context.OutputWriter.WriteLineAsync(message); | ||
| await GlobalContext.Current.OriginalConsoleOut.WriteLineAsync(message); | ||
| } | ||
| await WriteToOutputAsync(message, logLevel >= LogLevel.Error); | ||
| } | ||
|
|
||
| public override void Log<TState>(LogLevel logLevel, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
|
|
@@ -73,19 +69,18 @@ public override void Log<TState>(LogLevel logLevel, TState state, Exception? exc | |
|
|
||
| var message = GenerateMessage(formatter(state, exception), exception, logLevel); | ||
|
|
||
| if (logLevel >= LogLevel.Error) | ||
| { | ||
| context.ErrorOutputWriter.WriteLine(message); | ||
| GlobalContext.Current.OriginalConsoleError.WriteLine(message); | ||
| } | ||
| else | ||
| { | ||
| context.OutputWriter.WriteLine(message); | ||
| GlobalContext.Current.OriginalConsoleOut.WriteLine(message); | ||
| } | ||
| WriteToOutput(message, logLevel >= LogLevel.Error); | ||
| } | ||
|
|
||
| private string GenerateMessage(string message, Exception? exception, LogLevel logLevel) | ||
| /// <summary> | ||
| /// Generates the formatted message to be logged. | ||
| /// Override this method to customize the message format. | ||
| /// </summary> | ||
| /// <param name="message">The message to log.</param> | ||
| /// <param name="exception">The exception associated with this log entry, if any.</param> | ||
| /// <param name="logLevel">The log level.</param> | ||
| /// <returns>The formatted message.</returns> | ||
| protected virtual string GenerateMessage(string message, Exception? exception, LogLevel logLevel) | ||
| { | ||
| var stringBuilder = new StringBuilder(); | ||
|
|
||
|
|
@@ -120,4 +115,45 @@ private string GenerateMessage(string message, Exception? exception, LogLevel lo | |
|
|
||
| return builtString; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the message to the output. | ||
| /// Override this method to customize how messages are written. | ||
| /// </summary> | ||
| /// <param name="message">The formatted message to write.</param> | ||
| /// <param name="isError">True if this is an error-level message.</param> | ||
| protected virtual void WriteToOutput(string message, bool isError) | ||
| { | ||
| if (isError) | ||
| { | ||
| context.ErrorOutputWriter.WriteLine(message); | ||
| GlobalContext.Current.OriginalConsoleError.WriteLine(message); | ||
| } | ||
| else | ||
| { | ||
| context.OutputWriter.WriteLine(message); | ||
| GlobalContext.Current.OriginalConsoleOut.WriteLine(message); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously writes the message to the output. | ||
| /// Override this method to customize how messages are written. | ||
| /// </summary> | ||
| /// <param name="message">The formatted message to write.</param> | ||
| /// <param name="isError">True if this is an error-level message.</param> | ||
| /// <returns>A task representing the async operation.</returns> | ||
| protected virtual async ValueTask WriteToOutputAsync(string message, bool isError) | ||
| { | ||
| if (isError) | ||
| { | ||
| await context.ErrorOutputWriter.WriteLineAsync(message); | ||
| await GlobalContext.Current.OriginalConsoleError.WriteLineAsync(message); | ||
| } | ||
| else | ||
| { | ||
| await context.OutputWriter.WriteLineAsync(message); | ||
| await GlobalContext.Current.OriginalConsoleOut.WriteLineAsync(message); | ||
| } | ||
| } | ||
|
Comment on lines
+146
to
+158
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WriteToOutput method references the 'context' parameter directly instead of using the protected Context property. This creates an inconsistency since a protected Context property was added specifically for derived classes to access the context. Consider using 'Context' instead of 'context' for consistency with the extensibility design.