Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Last active August 29, 2015 14:11
Show Gist options
  • Save andreabalducci/82588da46549295dca88 to your computer and use it in GitHub Desktop.
Save andreabalducci/82588da46549295dca88 to your computer and use it in GitHub Desktop.
NHibernate: Test second level cache with Log4Net
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="WARN"/>
<appender-ref ref="RollingFile"/>
</root>
<logger name="NHibernate" additivity="false">
<level value="DEBUG" />
<appender-ref ref="NHibernateFileLog" />
</logger>
<logger name="NHibernate.Caches" additivity="false">
<level value="DEBUG" />
<appender-ref ref="NHibernateCachesFileLog" />
<appender-ref ref="MemoryCacheAppender" />
</logger>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\james.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-8ndc %-5thread %-5level %logger %message %n"/>
</layout>
</appender>
<appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
<file value="Logs/nhibernate.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="MemoryCacheAppender" type="log4net.Appender.MemoryAppender">
</appender>
<appender name="NHibernateCachesFileLog" type="log4net.Appender.RollingFileAppender">
<file value="Logs/nh-cache.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using James.Kernel.Model.Jobs;
using James.Tests.Support;
using James.Tests.Support.SampleData;
using log4net;
using log4net.Appender;
using NUnit.Framework;
namespace James.Tests.Mappings
{
public static class MemoryAppenderExtensions
{
public static bool CacheHitFor<T>(this MemoryAppender appender, object id)
{
string key = typeof(T).FullName + "#" + id;
return appender.GetEvents().Any(x =>
x.RenderedMessage.StartsWith("Fetching object") &&
x.RenderedMessage.Contains(key) &&
x.RenderedMessage.EndsWith("from the cache.")
);
}
}
[TestFixture]
public class SecondLevelCacheTests : TestOnSampleData
{
[Test]
public void job_should_be_read_from_second_level_cache()
{
var appender = (MemoryAppender)LogManager.GetRepository().GetAppenders()
.Single(x=>x.Name == "MemoryCacheAppender");
var job1 = Session.Get<Job>(SampleJob1.Id);
ResetSession();
appender.Clear();
var job2 = Session.Get<Job>(SampleJob1.Id);
Assert.AreNotSame(job1, job2);
Assert.IsTrue(appender.CacheHitFor<Job>(SampleJob1.Id));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment