As you know already I like base4 a lot
Alex Henderson has got a nice post on integrating base4 much prettier than I did in castle. Later on today I’ll post what I made of it
I tried to use log4net but didn’t succeed. So I switched to nlog which I like a lot.
Now to integrate nlog with base4 wasn’t all that hard.
<Type FullName=“NBlogr.Core.AppEvent“ baseName=“Base4.Storage.ItemImpl“ keyProperty=“Id“ version=“1“ mode=“Rich“>
<Property name=“ApplicationContext“ typeName=“Base4.Storage.ContentStream“ nullable=“True“ />
<Property name=“EventTime“ typeName=“System.DateTime“ />
<Property name=“Exception“ typeName=“System.String“ nullable=“True“ length=“1025“ />
<Property name=“Id“ typeName=“System.Guid“ unique=“True“ />
<Property name=“Level“ typeName=“System.String“ length=“256“ />
<Property name=“LoggerName“ typeName=“System.String“ length=“256“ />
<Property name=“Message“ typeName=“System.String“ length=“1025“ />
<Property name=“Sequence“ typeName=“System.Int32“ nullable=“True“ />
<Property name=“StackTrace“ typeName=“System.String“ nullable=“True“ length=“1025“ />
<Property name=“UserStackFrame“ typeName=“System.String“ nullable=“True“ length=“1025“ />
Type>
The actual target :
namespace NBlogr.Common.Base4Integration
{
///
/// This is a target for the nlog logging library.
/// This is dependent on the NBlogr.Core.xml file and Base4.
///
[Target("Base4Target")]
public class Base4Target : TargetWithLayout
{
private string _host = “tcp://server:@localhost:999/Base4_Default”;
public string Base4Context
{
get { return _host; }
set { _host = value; }
}
protected override void Write(LogEventInfo logEvent)
{
string logMessage = CompiledLayout.GetFormattedMessage(logEvent);
if(StorageContext.Default == null)
StorageContext.SetDefault(Base4Context);
AppEvent appEvent = new AppEvent();
appEvent.Sequence = logEvent.SequenceID;
if(logEvent.Context != null && logEvent.Context.Count > 0)
appEvent.ApplicationContext.Content = GetZippedBytes(logEvent.Context);
appEvent.EventTime = logEvent.TimeStamp;
if(logEvent.Exception != null) appEvent.Exception = logEvent.Exception.ToString();
if(logEvent.StackTrace != null) appEvent.StackTrace = logEvent.StackTrace.ToString();
if (logEvent.UserStackFrame != null) appEvent.UserStackFrame = logEvent.UserStackFrame.ToString();
appEvent.Level = logEvent.Level.Name;
appEvent.LoggerName = logEvent.LoggerName;
appEvent.Message = logMessage;
appEvent.Save();
}
private byte[] GetZippedBytes(IDictionary evt)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, evt);
GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress);
BinaryReader bRdr = new BinaryReader(zipStream);
return bRdr.ReadBytes((int)zipStream.Length);
}
}
}

