Using ConditionalWeakTable to tie logger lifetimes to service instances is a clever touch, nice idea, could the same pattern work cleanly for metrics collectors too?
Shadow Logging via Events - Complete Decoupling of Business Logic and Logging in DI Environment
2 Comments
Yes, the ConditionalWeakTable approach for binding metric collection to service instances is completely analogous to shadow logging and is ideal, providing complete decoupling of business logic from metrics.
ConditionalWeakTable<TKey, TValue> allows you to weakly bind metrics (e.g., IMetricsCollector) to a service as a key, without the risk of memory leaks—the collector is automatically deleted when the service is GC'd. It's important to note that the table requires unique keys, so if you have more than one service object for your key object (Logger, MetricsCollector, and so on), they should be stored in the table as a list of references, such as List< object >. In the article for loggers, this is implemented using a DI container (AddScopedWithLogger), and the same pattern is used: in the DI extension method, a table is created, a collector is added for each scoped service, and the business class generates events (such as MetricsRecorded) to which the shadow object subscribes.
Please log in to add a comment.
Please log in to comment on this post.
More Posts
- © 2026 Coder Legion
- Feedback / Bug
- Privacy
- About Us
- Contacts
- Premium Subscription
- Terms of Service
- Refund
- Early Builders
Related Jobs
- Supv Business Systems AnalystCity National Bank · Full time · Springfield, IL
- Business Development Manager - GPU and Neo-Cloud SalesImpossible Cloud · Full time · Canada
- Senior Business Systems AnalystLingaTech · Full time · Shady Grove, PA
Commenters (This Week)
Contribute meaningful comments to climb the leaderboard and earn badges!