Skip to content

Logging

Roger Kratz edited this page Dec 20, 2018 · 10 revisions

Introduction

You can create your own event listener for logging.

Here's an example using log4net.

public class MbCacheLog4NetListener : IEventListener
{
	private const string putMessage = 
		"Adding cache value {0} for {1})";
	private const string deleteMessage = 
		"Removing cache entries for {0}";
	private const string cacheHitLogMessage = 
		"Cache hit for {0}";

	private static readonly ILog log = LogManager.GetLogger("MbCache");

	public void OnCacheHit(CachedItem cachedItem)
	{
		if (log.IsDebugEnabled)
		{
			log.DebugFormat(cacheHitLogMessage, 
				cachedItem.CachedMethod.Name);
		}
	}

	public void OnCacheRemoval(CachedItem cachedItem)
	{
		if (log.IsDebugEnabled)
		{
			log.DebugFormat(deleteMessage, 
				cachedItem.CachedMethod.Name);
		}
	}

	public void OnCacheMiss(CachedItem cachedItem)
	{
		if (log.IsDebugEnabled)
		{
			log.DebugFormat(putMessage, 
				cachedItem.CachedValue, 
				cachedItem.CachedMethod.Name);
		}
	}
}
Clone this wiki locally