Thursday, April 17, 2008

Leveraging DotNetNuke Event Logging

We recently worked on a project where we were faced with logging some events that a custom module was creating. I decided to take a look at the DotNetNuke EventLogging API.




Here is some hacked together code that implemented this… Check out the sexy aggregate function at the bottom… :)

DotNetNuke.Services.Log.EventLog.EventLogController lc = new DotNetNuke.Services.Log.EventLog.EventLogController();
// Check if the logtype exists.
List logs = new List(lc.GetLogTypeInfo().Cast());
if ( !logs.Any(x => x.LogTypeKey == "IPVideo_Links") )
{
// If Not, create the logtype.
LogTypeInfo lt = new LogTypeInfo();
lt.LogTypeCSSClass = "IPVideo_Links";
lt.LogTypeDescription = "Logging the IPVideo Links Module";
lt.LogTypeFriendlyName = "IPVideo Links";
lt.LogTypeKey = "IPVideo_Links";
lt.LogTypeOwner = "DotNetNuke.Logging.EventLogType";
lc.AddLogType(lt);
}

// Add a log entry.
lc.AddLog(new LogProperties{
new LogDetailInfo("Property1", "Some Value 1"),
new LogDetailInfo("Property2", "Some Value 2"),
new LogDetailInfo("TimeSubmitted", DateTime.Now.ToString())
},
this.PortalSettings,
this.UserId,
"IPVideo_Links", /* Should probably make this a enum value */
true);

// Retrieve the events for the log.
int total = 0;
List details = new List(((LogInfo)lc.GetLog("IPVideo_Links", 100, 0, ref total)[0]).LogProperties.Cast());

// Get the events properties.
// I know you like my sexy use of Aggregate, admit it, its sexy.
string props = details.Aggregate("",
(result, logInfo) =>
String.Format("{0}{1}: {2}\n", result, logInfo.PropertyName, logInfo.PropertyValue));

// Show the properties.
txtDetails.Text = "Got the details\n" + props;







Here are some links I found useful when coming up with this code.

DNN Core API - Logging (pdf)

MSDN Aggregate Documentation