Skip to content

Commit fed6ff3

Browse files
author
Vadim Belov
committed
Simplify SimpleConsoleFormatter logic
Removed inline structured state rendering for non-exception logs to avoid duplication of template arguments. Simplified exception handling by consolidating logic into `WriteExceptionLines` and removing unused methods (`TryGetState`, `HasAnyPairs`, `WriteExceptionStateLine`, `WriteInlineState`, and `WriteKeyValue`). Updated `using` directives to reflect these changes. These updates streamline the codebase, improve maintainability, and focus on core logging functionality.
1 parent 86f37f6 commit fed6ff3

File tree

1 file changed

+4
-74
lines changed

1 file changed

+4
-74
lines changed

Sources/EasyExtensions.AspNetCore/Formatters/SimpleConsoleFormatter.cs

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Collections.Generic;
43
using Microsoft.Extensions.Options;
54
using Microsoft.Extensions.Logging;
65
using Microsoft.Extensions.Logging.Console;
@@ -50,42 +49,18 @@ public override void Write<TState>(in LogEntry<TState> logEntry,
5049
string msg = logEntry.Formatter?.Invoke(logEntry.State, logEntry.Exception) ?? string.Empty;
5150
textWriter.Write(msg);
5251

53-
// Non-exception: render structured state inline
54-
if (logEntry.Exception is null && TryGetState(logEntry.State, out var inlineKvps))
55-
{
56-
WriteInlineState(textWriter, inlineKvps, useAnsi);
57-
}
52+
// Do NOT render structured state inline for non-exception logs to avoid duplicating template args
53+
// (values are already substituted by the formatter into the message above).
5854

5955
textWriter.WriteLine();
6056

6157
// Exception: extra lines with [Ex]
62-
if (logEntry.Exception is Exception ex)
58+
if (logEntry.Exception != null)
6359
{
64-
if (TryGetState(logEntry.State, out var kvps) && HasAnyPairs(kvps))
65-
{
66-
WriteExceptionStateLine(textWriter, ts, level, displayCategory, kvps, useAnsi);
67-
}
68-
69-
WriteExceptionLines(textWriter, ts, level, displayCategory, ex, useAnsi);
60+
WriteExceptionLines(textWriter, ts, level, displayCategory, logEntry.Exception, useAnsi);
7061
}
7162
}
7263

73-
private static bool TryGetState<TState>(TState state, out IEnumerable<KeyValuePair<string, object>> kvps)
74-
{
75-
kvps = state as IEnumerable<KeyValuePair<string, object>>;
76-
return kvps is not null;
77-
}
78-
79-
private static bool HasAnyPairs(IEnumerable<KeyValuePair<string, object>> kvps)
80-
{
81-
foreach (var kv in kvps)
82-
{
83-
if (kv.Key == "{OriginalFormat}") continue;
84-
return true;
85-
}
86-
return false;
87-
}
88-
8964
private static void WriteHeader(TextWriter w, string ts, LogLevel level, string displayCategory, bool useAnsi)
9065
{
9166
w.Write('[');
@@ -133,35 +108,6 @@ private static void WriteExMarker(TextWriter w, bool useAnsi)
133108
w.Write("] ");
134109
}
135110

136-
private static void WriteInlineState(TextWriter w, IEnumerable<KeyValuePair<string, object>> kvps, bool useAnsi)
137-
{
138-
bool first = true;
139-
foreach (var kv in kvps)
140-
{
141-
if (kv.Key == "{OriginalFormat}") continue;
142-
if (first) { w.Write(" | "); first = false; } else { w.Write(", "); }
143-
WriteKeyValue(w, kv, useAnsi);
144-
}
145-
}
146-
147-
private static void WriteExceptionStateLine(TextWriter w, string ts, LogLevel level, string displayCategory,
148-
IEnumerable<KeyValuePair<string, object>> kvps, bool useAnsi)
149-
{
150-
WriteHeader(w, ts, level, displayCategory, useAnsi);
151-
WriteExMarker(w, useAnsi);
152-
w.Write("- ");
153-
154-
bool first = true;
155-
foreach (var kv in kvps)
156-
{
157-
if (kv.Key == "{OriginalFormat}") continue;
158-
if (!first) w.Write(", ");
159-
first = false;
160-
WriteKeyValue(w, kv, useAnsi);
161-
}
162-
w.WriteLine();
163-
}
164-
165111
private static void WriteExceptionLines(TextWriter w, string ts, LogLevel level, string displayCategory,
166112
Exception ex, bool useAnsi)
167113
{
@@ -176,22 +122,6 @@ private static void WriteExceptionLines(TextWriter w, string ts, LogLevel level,
176122
}
177123
}
178124

179-
private static void WriteKeyValue(TextWriter w, KeyValuePair<string, object> kv, bool useAnsi)
180-
{
181-
w.Write(kv.Key);
182-
w.Write('=');
183-
if (useAnsi)
184-
{
185-
w.Write(AnsiCyan);
186-
w.Write(kv.Value);
187-
w.Write(AnsiReset);
188-
}
189-
else
190-
{
191-
w.Write(kv.Value);
192-
}
193-
}
194-
195125
private static string ShortLevel(LogLevel level) => level switch
196126
{
197127
LogLevel.Trace => "TRC",

0 commit comments

Comments
 (0)