Writing to the Kentico Event Log

   —   

Proper logging and recording are at the core of every good Kentico project. Unless someone understands what the code is doing and has a record of the events, even the most well-designed and -architected solutions will frustrate users and admins and create a black hole of information within an organization. Luckily, Kentico has a number of supporting modules and utilities to help you document processes and log events quickly. In this blog, I’ll show you how to leverage logging to the Kentico Event Log within your code to get an accurate account of any processes and a record of events that occur within the system.

Within the Admin UI lies the Kentico Event Log. This simple module is a trusted friend to many developers and provides crucial insight into the system and what is happening within the code. Because this module is so integrated into the system, Kentico provides a very useful API to interact with the module and allow its use throughout the platform. Through the EventLogProvider API, you can quickly add your custom data to the Event Log and track changes and events from within your custom code.

Logging Exceptions

Chances are a developer will be logging exceptions to the Event Log as part of their custom code. With a simple line of code, the LogException method can deliver essential debugging information to the Log and allow developers to diagnose issues quickly.

EventLogProvider.LogException("Event Log Tester", "EXCEPTION", ex, CurrentSite.SiteID, "Additional exception message.");

Admins are well acquainted with spotting the “what did they blow up now?” red entries in the Event Log.

Log Exception 1

Log Exception 2

By passing the exception object, the Kentico API can easily extract the message, stack trace, and other essential information. Combined with the Settings/System/Error notification email addresses setting, admins can get notifications when custom code encounters an issue. You should be adding this anywhere you have a try/catch to be sure your errors are caught correctly.

Logging Warnings

Much like exceptions, warnings can be extremely helpful when a developer needs to log that something is not right. With the LogWarning method, you can log any warnings to the Event Log that can be easily viewed by admins if an issue arises.

EventLogProvider.LogWarning("Event Log Tester", "WARNING", ex, CurrentSite.SiteID, "Additional warning message.");

Because the event is marked as a “warning”, it’s easy to spot in the Log with its “this is kind of alarming but only a little” yellow background.

Log Warning 1

Log Warning 2

I often have used the LogWarning method to record an event that potentially may affect another business process but doesn’t necessarily bring the code to a halt.

Logging Information

Sometimes, developers just want to log information to the Event Log without causing alarm. With the LogInformation method, the Log can be updated with whatever information is needed and can then recorded appropriately.

EventLogProvider.LogInformation("Event Log Test", "INFORMATION", "Sample information message.");

Information entries are not quite as flashy and rarely cause a stir (unless you REALLY don’t like any events in the Log).

Log Information 1

Log Information 2

In the past, I have used this code within a Scheduled Task or module to record the processed data.

Logging General Events

If none of the type-specific functions meets your needs, the EventLogProvider API also contains the LogEvent method. This is a more “hands on” type of call, allowing you to specify every aspect of the logging.

EventLogProvider.LogEvent(EventType.ERROR, "Event Log Tester", "GENERAL", eventDescription: "General Message: " + ex.Message + ", StackTrace:" + ex.StackTrace);

Depending on how you specify the EventType, entries will be white, yellow, or red.

Log General 1

Log General 2

Typically, I leverage the type-specific methods for logging information. On occasion, I have needed to document every aspect of an event fully, which is where the LogEvent method comes in handy because it allows you to set the parameters of the object.

Bonus: Clearing the Event Log

If you need to clear the Event Log and don’t feel like logging into the Admin section (like when you’re making a demo web part for demonstrating working with the Event Log for a blog), you can use the ClearLog method to clear entries quickly.

EventLogProvider.ClearEventLog(CurrentUser.UserID, CurrentUser.UserName, "127.0.0.1", EventLogProvider.ALL_SITES);

Note that you need to supply the site to the method so Kentico knows which events to clear. If you want to clear them all, you can pass the ALL_SITES constant to clear all Log entries.

Clear Log 1

Clear Log 2

Wrapping Up

Using the Kentico Event Log within your custom code is a great way to add logging to your functionality with a line or two of code. Once the records are logged, you can leverage all of the built-in interfaces and modules to manage and track your data properly. Good luck!

Share this article on   LinkedIn Google+

Bryan Soltis

Hello. I am a Technical Evangelist here at Kentico and will be helping the technical community by providing guidance and best practices for all areas of the product. I might also do some karaoke. We'll see how it the night goes...

Comments