-
Notifications
You must be signed in to change notification settings - Fork 101
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
String Quoting - global disable switch #253
Comments
Hi, thanks for the ping. That doesn't look like console output - are you using a different sink? |
I'm sorry @nblumhardt, that was misleading. Here's more info, which I hope will prove more useful. Serilog initialization:
I additionally configured the OpenTelemetry ILogger: _ = builder.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.AddOpenTelemetry(o =>
{
o.IncludeFormattedMessage = true;
o.IncludeScopes = true;
});
}); More examples: Custom log
Aspire dashboard output for reference - it's the same formatted message, I assumed the separated non-Serilog OTel exporter is piggybacking on Serilog: ASP.NET Core log, this should be the original string format: https://github.com/dotnet/aspnetcore/blob/release/8.0/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs#L12
Thank you. |
Thanks; I see 👍 This is the Serilog.Sinks.Console repo, which doesn't play a part in your setup. For Serilog sinks, the way to disable quoting is to use the I think the Serilog.Extensions.Logging project would be the most likely place this could be addressed, if it turns out to be possible. I'll transfer the issue there and hopefully someone will spot it and take a look at what's involved. Cheers! |
Thanks, @nblumhardt. I just realized the console output was duplicated: both Serilog's Console sink and MEL's Console sink were outputting to stdout - hence the wrong screenshot above; apologies for the confusion. Serilog's Console rendering looks fine. I amended the configuration:
Before calling
with By the way, this is the configuration I'm using so I assumed it was {
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}: {Message:lj}{NewLine}{Exception}"
}
}
]
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
]
}
} Odd enough, I enabled a custom formatter inheriting from Configuration:
The formatter - it's not much at all, really internal sealed class ElasticStackFormatter
: ExceptionAsObjectJsonFormatter
{
public ElasticStackFormatter(bool omitEnclosingObject = false,
string? closingDelimiter = null,
bool renderMessage = true,
IFormatProvider? formatProvider = null,
ISerializer? serializer = null,
bool inlineFields = false,
bool renderMessageTemplate = true,
bool formatStackTraceAsArray = false)
: base(omitEnclosingObject,
closingDelimiter,
renderMessage,
formatProvider,
serializer,
inlineFields,
renderMessageTemplate,
formatStackTraceAsArray)
{
}
} The output: {
"@timestamp": "2024-06-19T15:52:44.9203512+02:00",
"level": "Information",
"messageTemplate": "Request starting {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {ContentType} {ContentLength}",
"message": "Request starting \"HTTP/1.1\" \"GET\" \"http\"://\"localhost:3244\"\"\"\"/swagger/v1/swagger.json\"\"\" - null null",
"fields": {
"Protocol": "HTTP/1.1",
"Method": "GET",
"ContentType": null,
"ContentLength": null,
"Scheme": "http",
"Host": "localhost:3244",
"PathBase": "",
"Path": "/swagger/v1/swagger.json",
"QueryString": "",
"EventId": {
"Id": 1
},
"SourceContext": "Microsoft.AspNetCore.Hosting.Diagnostics",
"RequestId": "0HN4GCSC288FE:00000002",
"RequestPath": "/swagger/v1/swagger.json",
"ConnectionId": "0HN4GCSC288FE",
"MachineName": "NOTEBOOK",
"ProcessId": 33980,
"ThreadId": 19
}
} Still, it would be nice to format the message once for all downstream consumers, as it were. |
Hi @rcollina, The quoted string output is produced by the default Serilog.Exprssions provides a flexible formatter that can be used in a variety of situations, and implements the same interface as your custom formatter above. The Serilog.Expressions formatter option looks like: // dotnet add package Serilog.Expressions
var formatter = new ExpressionTemplate(
"{ { '@timestamp': @t, level: @l, message: @m, messageTemplate: @mt, fields: @p, exception: Inspect(@x) } }"); Aside from that I think we'll look at making it easier for components/sinks to use the more modern rendering style - some work is needed though. |
Hello @nblumhardt, {
"@timestamp": "2024-06-21T12:49:58.8373640+02:00",
"level": "Information",
"message": "Request starting HTTP/1.1 GET http://localhost:3244/swagger/v1/swagger.json - null null",
"messageTemplate": "Request starting {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {ContentType} {ContentLength}",
"fields": {
"Protocol": "HTTP/1.1",
"Method": "GET",
"ContentType": null,
"ContentLength": null,
"Scheme": "http",
"Host": "localhost:3244",
"PathBase": "",
"Path": "/swagger/v1/swagger.json",
"QueryString": "",
"EventId": {
"Id": 1
},
"SourceContext": "Microsoft.AspNetCore.Hosting.Diagnostics",
"RequestId": "0HN4HRVI3MEPK:00000002",
"RequestPath": "/swagger/v1/swagger.json",
"ConnectionId": "0HN4HRVI3MEPK",
"MachineName": "NOTEBOOK",
"ProcessId": 23312,
"ThreadId": 10
}
} However, this has no impact on what gets passed to MEL ILoggers - so I assume serilog-extensions-logging/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs Line 89 in 1e9f655
|
JSON output looks good! Yes, this repo will need changes if we're to pass the cleaner-style messages through to MEL providers. |
Collecting some notes; serilog/serilog-sinks-opentelemetry#50 is another case where clean formatting has been implemented; the amount of code involved makes a pretty strong case for adding a core Serilog API to do this. |
Serilog.Sinks.Console 5.0.0
viaSerilog.AspNetCore 8.0.1
dotnet
toolchain version, target framework, and operating system -dotnet sdk 8.0.205
,dotnet 8
Hello,
I can't for the life of me find a way to globally disable automatic string quoting, i.e.:
This is my current
outputTemplate
:[{Timestamp:HH:mm:ss} {Level}] {SourceContext}: {Message:lj}{NewLine}{Exception}
What am I doing wrong?
Thanks for all and any input.
The text was updated successfully, but these errors were encountered: