[BUG] Unwanted creation of HttpContextAccessor
for enrichment
#471
Labels
bug
Something isn't working
HttpContextAccessor
for enrichment
#471
ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog): Elastic.Serilog.Enrichers.Web
ECS schema version (e.g. 1.4.0): N/A
ECS .NET assembly version (e.g. 1.4.2): N/A
Elasticsearch version (if applicable): N/A
.NET framework / OS: All
Description of the problem, including expected versus actual behavior:
Your docs at https://github.com/elastic/ecs-dotnet/blob/main/src/Elastic.Serilog.Enrichers.Web/README.md suggest the following:
Yet
ctx.Configuration.Get<T>()
is intended for binding configuration.HttpContextAccessor
is not config.In other samples, and implied via the
// Ensure HttpContextAccessor is accessible
comment, the use of the following is suggested soHttpContextAccessor
can be resolved via your configuration get call:These are two completely different things and your recommended code is misleading and causes unwanted behaviour.
Using the
AddHttpContextAccessor
helper to register a singleton instance ofIHttpContextAccessor
means the service container will be in control of the instance that is instantiated. Therefore, it will also dispose cleanly if needed (I appreciate the interface does not implementIDisposable
in this case).Accessing the
HttpContextAccessor
in the way you are recommending it be done leads to an additional instance being instantiated. It is logically equivalent to suggesting to usenew HttpContextAccessor()
instead as all that happens isActivator.CreateInstance
is called.Is there a reason you're doing it in this way that I have missed?
Otherwise I would suggest using your main
UseSerilog
overload instead that would look like as follows:Here you are resolving the
IHttpContextAccessor
instances correctly.The text was updated successfully, but these errors were encountered: