DNN - Create Custom Event Log Type Entries

Logging of events, success or failures, during life of an application is very integral part of diagnostics. When something goes wrong happens first place I would like to go is to some log table (database, file etc.) to find out what went wrong. I am not going to discuss what it is important to implement logging mechanism in your application. There are plenty of books, article and posts that provide some good discussion on this design decision. In this post I am going to focus on how do you use logging and provide custom events. DotnetNuke provides built in mechanism for logging of all the events. You can look at these events under Admin > Event Viewer menu when you log in with admin or host account.

When you look at the event viewer page you will find lot of entries. Some of those entries are informative and some would be related to exceptions and errors that occur during life of application. Let me first show how these entries make it to the tables in the database or any logging mechanism in DNN. Following code snippet shows the simplest implementation that you can put in your DNN module to log some entries.

var logController = new EventLogController();
var logInfo = new LogInfo();
logInfo.LogPortalID = portalSettings.PortalId;
logInfo.BypassBuffering = true;
logInfo.LogTypeKey = logType;
logInfo.AddProperty("Message", message);

I will describe this code in details as I discuss the logging mechanism during the course of this post. You can see how easy it is to add entries in the event log of DNN portal. The key to this code snippet and logging of event is LogTypeKey property of LogInfo object. When you look at event log, there is a column named Log Type in the grid as you can see from the image below.

DNN Event Log Viewer

You will notice that above the log grid, there is Type dropdown list. If you look at all entries in this list, you will find that the names in this list match some of the entries in the event log. Yes, each entry in the event log is associated with these types. This means that you just can not have som arbitrary name specified for log type entry. Each of these entries have a unique name associated with it which acts as LogTypeKey. Look at the tables for your DNN installation and you will find two tables dnn_EventLogTypes and dnn_EventLogConfig that control the logging mechanism. If you look at the entries in dnn_EventLogTypes, you will find all the names in Types dropdown list in LogTypeFriendlyName field. And there is a field named LogTypeKey that stores unique names for each of the log type. And for each of these LogTypeKey values there are entries in dnn_EventLogConfig table that define attributes for each of these log types. Most importnat of the fields in that table is LoggingIsActive. This is the field that control if entry for that type of log will be entered in the database or not.

DNN Event Log Types

If you specify value of LogTypeKey that does not exist in dnn_EventLogTypes table, the call for AddLog method on EventLogController simply returns without adding any entry in the database for the message that you are trying to add. If you want to use the built in event types, then you can use simply use of the values from DotNetNuke.Services.Log.EventLog.EventLogType enum.

Color Coding Of Entries In Event Log

When you look at entries in event viewer grid, you will notice that different event types are represented with different color or styles. These colors or styles come from LogTypeCSSClass field in dnn_EventLogTypes tables. You will notice heading Color Coding Legend above the event log grid. If you click on + icon on left, it will expand to a section showing colors for different log type entries as show in image below.

DNN Event Log Type Color Styles

Custom Event Log Type In DotnetNuke

When you are doing custom module development for DotnetNuke, then you want to have entries in the log that are specific to your module only. By doing this, it makes it very easy for you to filter the view to see what all errors or activities are present in the event log for your module. To add custom entries in event log, you have to perform following two steps before you could use AddLog method to add log entries.

Add New Log Type Key

Pick a unique name that will be used as key for your custom log entry type. And then decide on a friendly name that will be displayed in the event log view for custom event. Then you can use following code to add your log type.

var logController = new LogController();
var logTypeInfo = new LogTypeInfo();
logTypeInfo.LogTypeCSSClass = "MyEventCssClass";
logTypeInfo.LogTypeDescription = description;
logTypeInfo.LogTypeFriendlyName = "My friendly name for event";
logTypeInfo.LogTypeOwner = "DotNetNuke.Logging.EventLogType";
logTypeInfo.LogTypeKey = "My Module Event Key"; // Pick unique key name

In the code above replace the values for LogTypeCSSClass, LogTypeKey, LogTypeFriendlyName and LogTypeDescription properties with the ones that you want to use for your custom event. You can not add the same key twice. If you call AddLogType with same LogTypeKey again, you will end up with following error.

Violation of PRIMARY KEY constraint 'PK_dnn_EventLogTypes'. Cannot insert duplicate key in object 'dbo.dnn_EventLogTypes'.

Activate Custom Event Type

After you have added your custom log type, you will need to set its attribute to make it active for logging. This is done by using AddLogTypeConfigInfo method on LogController. Following code snippet shows some code from my module.

var logTypeConfigInfo = new LogTypeConfigInfo();
logTypeConfigInfo.LogTypeKey = logTypeKey;
logTypeConfigInfo.LoggingIsActive = true;
logTypeConfigInfo.MailFromAddress = mailserver;
logTypeConfigInfo.MailToAddress = mailTo;
logTypeConfigInfo.EmailNotificationIsActive = false;

The most important property of LogTypeConfigInfo object is LoggingIsActive. If you do not set this property, your custom log type will stay inactive and any AddLog method call will be ignored. You can call AddLogTypeConfigInfo multiple types. The underlying stored procedure checks for existing settings. If there is already one for that log type, it simply updates the vlaues in the table.

Custom Event Log Type Ready!

One you have performed above two steps, you are all set to use your custom event log type in DNN module. Since you can add entries in dnn_EventLogTypes only one time, you will have to make sure that you do not attempt to add more than one time. Check if certain log type already exists before adding. Following code snippet from my custom module shows how it can be done.

public static bool LogTypeKeyInstalled(string logTypeKey)
   var eventLogController = new EventLogController();
   var logTypes = new List<LogTypeInfo>
   return logTypes.Any(lt => lt.LogTypeKey == logTypeKey);

if (!LogTypeKeyInstalled("LOAD_FAIL"))
 // Perform add operation for custom log type

Now we are all set with custom log type!




18.8 °C / 65.8 °F

weather conditions Clear

Monthly Posts

Blog Tags