Closed yohny closed 1 year ago
Serilog uses FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
, not AssemblyQualifiedName, see https://github.com/serilog/serilog/blob/7f1e791c411457eb392a9b4833a5545a90a67ba7/src/Serilog/Core/Logger.cs#L164
I added repro project at https://github.com/yohny/SerilogRefiRepro where the logging issue can be observed
See
and
This is refit implementation of http client name. Then this name will be mapped/used as Serilog's SourceContext
somewhere in internals of Serilog.Extensions.Logging
package IMO. Note that you inject MEL ILogger
instead of Serilog's ILogger
.
@nblumhardt may help more here than I.
Serilog maps MEL's logger name to SourceContext
. It seems that this isn't always a type name.
In Serilog also, SourceContext
is just a strign, it's not strictly defined as always being a FullName
but that's the general default.
You can work around this by implementing an ILogEventEnricher
that you plug in using Enrich.With(...)
; in the enricher, you can first retrieve the SourceContext
property, trim it as required, and then update it back on the LogEvent
.
Hope this helps!
ok, I see that my issue is not actually caused by Serilog lib, thanks for answers
I have an ASP.NET 6 web API project that uses Serilog. The logging is configured to with
app.UseSerilogRequestLogging()
and with customized message template, which includes{SourceContext}
in addition to default. The log messages with one exception have expected value for{SourceContext}
, which is the type name, likeMyAssembly.MyClass
. The one case where the{SourceContext}
value is a bit unexpected has to do with Refit.Normally when making request from controller to some external API using normal
httpClient
the log looks as follows:However when makig a request using Refit (aka. runtime generated code), the log looks like this:
The problem being that instead of just type name, there appears to be fully qualified type name (including assembly, version, culture and public token)
Describe the solution you'd like I would prefer if only the type name would be in
SourceContext
also for generated code, to avoid unnecessary log pollution and to be consistent with standard httpClient logging.Describe alternatives you've considered I tried implementing custom
Logger
as suggested here https://stackoverflow.com/questions/48470090/serilog-format-sourcecontext-for-showing-only-assembly-name, but it had no effect onSourceContext
of generated refit code.Additional context Serilog
2.12.0
, Serilog.AspNetCore6.1.0
repository with repro project https://github.com/yohny/SerilogRefiRepro - when launched go toswagger/
where there are 2 endpoints exposed (one making external request using httpClient, the other using refit) - try them and observe the console log