dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

Need more information on when and how to use this interface

Open mtaylorfsmb opened this issue 11 months ago • 6 comments

Type of issue

Missing information

Description

This interface appears to be useful for capturing additional information into a structured form usable by structured loggers. But there is no documentation on the actual usage of this interface nor any examples. The only reason I found this is because of the extension method, that is also not well documented, over here.

It would be nice to get some discussion on when this is useful and what to do with it. While I can see where it is possible to add a new enricher, the IEnrichmentTagCollector that enrichers write to doesn't seem to be accessible anywhere else. So if I wanted to use an enricher to add some data to logging then I could create my own enricher. But I don't see any way to later write a logger, or whatever, that then has access to this collection of enriched data.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.http.logging.ihttpclientlogenricher?view=net-9.0-pp

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/Microsoft.Extensions.Http.Logging/IHttpClientLogEnricher.xml

Document Version Independent Id

6a9aad43-d09a-0706-1a59-3c44856344a9

Platform Id

bddae806-f751-afd0-bc13-29d59da12b47

Article author

@dotnet-bot

mtaylorfsmb avatar May 30 '25 14:05 mtaylorfsmb

CC @evgenyfedorov2 @geeknoid

tarekgh avatar May 30 '25 15:05 tarekgh

It would be nice to get some discussion on when this is useful and what to do with it.

This is useful for creating custom log enricher, as you say below.

While I can see where it is possible to add a new enricher, the IEnrichmentTagCollector that enrichers write to doesn't seem to be accessible anywhere else. So if I wanted to use an enricher to add some data to logging then I could create my own enricher. But I don't see any way to later write a logger, or whatever, that then has access to this collection of enriched data.

If you create a custom enricher, you then register it with IServiceCollection using the extension method, and you don't have to do anything else. The logging infra will call the Enrich method at the right time, extract all information from IEnrichmentTagCollector, and add it to log record's state. As a result, your logs will have the key/value pairs your custom enricher enriched them with.

Does this answer your questions, @mtaylorfsmb ?

evgenyfedorov2 avatar Jul 15 '25 11:07 evgenyfedorov2

@evgenyfedorov2, thank you for the additional information. You have answered part of my question. From the producer side it makes sense that the infrastructure calls the enricher. But what about the consumer side? We have custom loggers that would like access to this data. Where is this collection, or wherever it is ultimately boiled up to, accessible from the logger implementation side?

My point of this report is that this is yet another interface that seems useful but there is literally no documentation about it at all. It seems like someone at MS, or perhaps a partner, had a need for this so it was added. But without any documentation then it seems that nobody other than the requestor and implementers would be able to use it. If this is not designed for us to use then it should probably be marked as infrastructure and can remain undocumented. Otherwise at least a basic set of documentation on how/when to implement the interface and how/when to consume the interface would be useful.

Thanks.

mtaylorfsmb avatar Jul 15 '25 13:07 mtaylorfsmb

But what about the consumer side? We have custom loggers that would like access to this data. Where is this collection, or wherever it is ultimately boiled up to, accessible from the logger implementation side?

It depends on your use case.

  • One example is here https://source.dot.net/#Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs,42 - enrichers are injected via DI, and then enrichment data attached to log record state https://source.dot.net/#Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs,238
  • If you have a custom logger/logger provider, you normally have access to the TState state which contains all key/value pairs including those added by enrichers, so you can do type-checking for IReadOnlyList<> and enumerate over it

If this is not designed for us to use then it should probably be marked as infrastructure and can remain undocumented.

This is designed to be used mainly on the consumer side. But yes, I can't agree more - we need to do a better job at documenting it.

evgenyfedorov2 avatar Jul 15 '25 13:07 evgenyfedorov2

@evgenyfedorov2 Thank you for the additional information.

mtaylorfsmb avatar Jul 15 '25 14:07 mtaylorfsmb

cc @mariamgerges

evgenyfedorov2 avatar Dec 15 '25 13:12 evgenyfedorov2