I log visualizzati in CMTrace mostravano entità HTML invece dei caratteri speciali:
? Active Sinks: "AzureQueue, WebApi"
? Value: x < 10 && y > 5
? SQL: SELECT * WHERE name = 'test'
Il CMTraceFormatter stava facendo escape dei caratteri XML prima di scriverli nel log:
// ? ERRATO
private static string EscapeXml(string text)
{
return text
.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\"", """)
.Replace("'", "'");
}
output.Write(EscapeXml(message)); // ? Causa ", <, etc.Il tag <![LOG[...]LOG]!> in CMTrace è una sezione CDATA-like che:
- ? Gestisce automaticamente i caratteri speciali
- ? Non richiede escape XML
- ? L'escape causa la visualizzazione delle entità HTML
Rimosso l'escape XML dal formatter:
// ? CORRETTO
public void Format(LogEvent logEvent, TextWriter output)
{
var message = logEvent.RenderMessage();
if (logEvent.Exception != null)
{
message = $"{message}{Environment.NewLine}{logEvent.Exception}";
}
// ? NON fare escape! Il tag <![LOG[...]LOG]!> è CDATA-like
output.Write("<![LOG[");
output.Write(message); // ? Scritto direttamente
output.Write("]LOG]!>");
// ... resto del formato
}Sending HTTP request "GET" "https://example.com"
Error: x < 10 && y > 5
Path: C:\Users\test
SQL: name = 'test'
Sending HTTP request "GET" "https://example.com"
Error: x < 10 && y > 5
Path: C:\Users\test
SQL: name = 'test'
Rimosso:
private static string EscapeXml(string text) { ... } // ? RimossoAggiunto commento:
// NOTE: Do NOT escape XML characters in the message.
// The <![LOG[...]LOG]!> is a CDATA-like section and CMTrace handles special characters correctly.
// Escaping them causes ", <, etc. to appear in the log viewer.# Genera nuovi log
cd SecureBootWatcher.Client\bin\Debug\net48
.\SecureBootWatcher.Client.exe
# Verifica che NON ci siano entità HTML
Get-Content "logs\client-*.log" | Select-String -Pattern '"|<|>|'|&'Output atteso: Nessun risultato ?
CMTrace.exe "logs\client-*.log"Verifica che:
- ? Virgolette appaiono come
"non" - ? Apostrofi appaiono come
'non' - ? Simboli
<e>appaiono correttamente - ? Ampersand
&appare come&
cd SecureBootWatcher.Client
dotnet clean
dotnet build -c Release- ? Il formato CMTrace rimane il default
- ? Nessun cambio in
appsettings.json - ? Compatibile con deployment esistente
- ? Formato CMTrace rimane compatibile
- ? Colorazione funziona come prima
- ? Filtri e ricerca funzionano
- ? Migliore leggibilità dei log
- ? Leggermente più veloce (no escape overhead)
- ? Stesso footprint di memoria
- ? Stessa dimensione file
Tutti i caratteri speciali ora appaiono correttamente:
| Carattere | Prima | Dopo |
|---|---|---|
" |
" ? |
" ? |
' |
' ? |
' ? |
< |
< ? |
< ? |
> |
> ? |
> ? |
& |
& ? |
& ? |
\ |
\ ? |
\ ? |
? Caratteri speciali mostrati come entità HTML
? Log poco leggibili in CMTrace
? Escape XML non necessario
? Rimosso escape XML dal formatter
? Caratteri speciali visualizzati correttamente
? Log più leggibili
? Performance leggermente migliorate
Status: ? CORRETTO
Build Required: Sì (rebuild client)
Config Changes: No
Breaking Changes: No
Documentation: Aggiornata