|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using log4net.Core;
using log4net.Appender;
using log4net.Repository;
namespace Tests
{
public delegate void CacheLogAppendDelegate(CachedEntityInfo cacheInfo);
/// <summary>
/// Log4Net appender implementation
/// </summary>
public class CacheLogAppender : AppenderSkeleton
{
/// <summary>
/// Cache action enumeration.
/// </summary>
public enum CacheActionType
{
Unknow,
Invalidate,
Release,
Caching,
Cached,
CacheLookup,
CacheMiss,
CacheHit,
Locked,
Inserting,
Inserted
}
private CacheLogAppendDelegate _onLogAppend;
public event CacheLogAppendDelegate OnLogAppend
{
add { _onLogAppend += value; }
remove { _onLogAppend -= value; }
}
/// <summary>
/// Parse CacheActionType from name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static CacheActionType ParseCacheActionTypeFromName(string name)
{
string invariantName = name.ToLowerInvariant();
switch (invariantName)
{
case "invalidating":
return CacheActionType.Invalidate;
case "releasing":
return CacheActionType.Release;
case "caching":
return CacheActionType.Caching;
case "cached":
return CacheActionType.Cached;
case "cache lookup":
return CacheActionType.CacheLookup;
case "cache miss":
return CacheActionType.CacheMiss;
case "cache hit":
return CacheActionType.CacheHit;
case "cached item was locked":
return CacheActionType.Locked;
case "inserting":
return CacheActionType.Inserting;
case "inserted":
return CacheActionType.Inserted;
default:
return CacheActionType.Unknow;
}
}
/// <summary>
/// Append log
/// </summary>
/// <param name="loggingEvent"></param>
protected override void Append(LoggingEvent loggingEvent)
{
if (loggingEvent.MessageObject == null)
return;
CachedEntityInfo cachedEntity = ParseMessageObject(loggingEvent.MessageObject);
if(cachedEntity != null && _onLogAppend != null)
_onLogAppend(cachedEntity);
}
/// <summary>
/// Append logs
/// </summary>
/// <param name="loggingEvents"></param>
protected override void Append(LoggingEvent[] loggingEvents)
{
base.Append(loggingEvents);
}
/// <summary>
/// Parse message object to produce a CachedEntityInfo instance.
/// </summary>
/// <param name="messageObject"></param>
/// <returns></returns>
private CachedEntityInfo ParseMessageObject(object messageObject)
{
if (messageObject == null)
throw new NullReferenceException("Message object is null. Can not parse log message object.");
string logMessage = messageObject.ToString();
Match m = Regex.Match(logMessage, @"(?<ActionName>.*)\s*:\s*(?<TypeName>.*)\s*\#\s*(?<Id>.*)", RegexOptions.IgnoreCase);
if (!m.Success)
throw new Exception("Log message does not match the pattern!");
string actionName = m.Groups["ActionName"].Value;
string typeName = m.Groups["TypeName"].Value;
string id = m.Groups["Id"].Value;
return new CachedEntityInfo(String.IsNullOrEmpty(typeName) ? typeName : typeName.Trim()
, String.IsNullOrEmpty(id) ? id : id.Trim()
, String.IsNullOrEmpty(actionName) ? actionName : actionName.Trim());
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.