Skip to content

Instantly share code, notes, and snippets.

@lgolubyev
Created June 2, 2022 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgolubyev/c2a6b4a89d715921ae4028a09f7f2d41 to your computer and use it in GitHub Desktop.
Save lgolubyev/c2a6b4a89d715921ae4028a09f7f2d41 to your computer and use it in GitHub Desktop.
// when using `services.AddMemoryCache(options => options.TrackStatistics = true);` to instantiate
[EventSource(Name = "Microsoft-Extensions-Caching-Memory")]
internal sealed class CachingEventSource : EventSource
{
public CachingEventSource(IMemoryCache memoryCache) { _memoryCache = memoryCache; }
protected override void OnEventCommand(EventCommandEventArgs command)
{
if (command.Command == EventCommand.Enable)
{
if (_cacheHitsCounter == null)
{
_cacheHitsCounter = new PollingCounter("cache-hits", this, () =>
_memoryCache.GetCurrentStatistics().CacheHits)
{
DisplayName = "Cache hits",
};
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment