Kentico CMS 6.0 Developer's Guide

Managing log events

Managing log events

Previous topic Next topic Mail us feedback on this topic!  

Managing log events

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example logs an event in the event log.

 

private bool LogEvent()

{

  // Create new event object

  EventLogInfo newEvent = new EventLogInfo();

 

  // Set the properties

   newEvent.EventType = "I";

   newEvent.EventDescription = "My new logged event.";

   newEvent.EventCode = "APIEXAMPLE";

   newEvent.EventTime = DateTime.Now;

   newEvent.Source = "API Example";

   newEvent.SiteID = CMSContext.CurrentSiteID;

 

  // Create new instance of event log provider

  EventLogProvider eventLog = new EventLogProvider();

 

  // Log the event

   eventLog.LogEvent(newEvent);

 

  return true;

}

 

The following example gets and updates the event created by the example above.

 

private bool GetAndUpdateEvent()

{

  // Create new instance of event log provider

  EventLogProvider eventLog = new EventLogProvider();

 

  // Get top 1 event matching the where condition

  string where = "EventCode = 'APIEXAMPLE'";

  int topN = 1;

  DataSet events = eventLog.GetAllEvents(where, null, topN, null);

 

  if (!DataHelper.DataSourceIsEmpty(events))

   {

      // Create the object from DataRow

      EventLogInfo updateEvent = new EventLogInfo(events.Tables[0].Rows[0]);

     

      // Update the properties

       updateEvent.EventDescription = updateEvent.EventDescription.ToLower();

                 

      // Save the changes

       eventLog.SetEventLogInfo(updateEvent);

                 

      return true;

   }

 

  return false;

}

 

The following example gets multiple events based on a WHERE condition and bulk updates them.

 

private bool GetAndBulkUpdateEvents()

{

  // Create new instance of event log provider

  EventLogProvider eventLog = new EventLogProvider();

 

  // Get events matching the where condition

  string where = "EventCode = 'APIEXAMPLE'";

  DataSet events = eventLog.GetAllEvents(where, null);

 

  if (!DataHelper.DataSourceIsEmpty(events))

   {

      // Loop through the individual items

      foreach (DataRow eventDr in events.Tables[0].Rows)

       {

          // Create the object from DataRow

          EventLogInfo updateEvent = new EventLogInfo(eventDr);

 

          // Update the properties

           updateEvent.EventDescription = updateEvent.EventDescription.ToUpper();

 

          // Save the changes

           eventLog.SetEventLogInfo(updateEvent);

       }

 

      return true;

   }

 

  return false;

}

 

The following example clears the event log.

 

private bool ClearLog()

{

  // Create new instance of event log provider

  EventLogProvider eventLog = new EventLogProvider();

 

  // Clear event log for current site

   eventLog.ClearEventLog(CMSContext.CurrentUser.UserID, CMSContext.CurrentUser.UserName, HTTPHelper.GetUserHostAddress(), CMSContext.CurrentSiteID);

 

  return true;

}