Event log
List of examples:
- Logging events
- Logging events using LogEvent
- Working with events (sending events by email)
- Clearing the event log
Logging events
IEventLogService eventLog = Service.Resolve<IEventLogService>();
// In case of database outage, the system buffers all logged events in-memory until the database is available.
// You do not need to buffer or check for database availability in custom code.
// Logs an information event into the event log
eventLog.LogInformation("API Example Info", "APIEXAMPLE", eventDescription: "Test information event.");
// Logs a warning event into the event log
eventLog.LogWarning("API Example Warning", "APIEXAMPLE", eventDescription: "Test warning event.");
// Logs an error event into the event log
eventLog.LogError("API Example Error", "APIEXAMPLE", eventDescription: "Test error event.");
// Logs an exception into the event log
eventLog.LogException("API Example Exception", "APIEXAMPLE", new Exception(), additionalMessage: "Test exception event.");
Logging events using LogEvent
IEventLogService eventLog = Service.Resolve<IEventLogService>();
// Prepares an EventLogData object that holds all information about the event being logged
EventLogData eventData = new EventLogData(EventTypeEnum.Information, "API Example", "APIEXAMPLE")
{
SiteID = SiteContext.CurrentSiteID,
EventTime = DateTime.Now,
EventDescription = "Logging a sample event to the event log."
};
// Logs the event into the event log
eventLog.LogEvent(eventData);
Working with events (sending events by email)
// Gets all error events logged in the past day
var errors = EventLogInfo.Provider.Get()
.WhereEquals("EventType", EventType.ERROR)
.WhereGreaterThan("EventTime", DateTime.Now.Subtract(TimeSpan.FromDays(1)));
if (errors.Count > 0)
{
// Creates the email message
EmailMessage msg = new EmailMessage()
{
From = "system@localhost.local",
Recipients = "admin@localhost.local",
Subject = "Xperience Errors (" + errors.Count + ")",
Body = "<html><body><ul>"
};
// Creates a list of the errors
foreach (EventLogInfo errorEvent in errors)
{
msg.Body += String.Format("<li>{0} - {1} - {2}</li>", errorEvent.EventType, errorEvent.EventCode, errorEvent.EventDescription.Substring(0, 100));
}
msg.Body += "</ul></body></html>";
// Sends out the email message
EmailSender.SendEmail(msg);
}
Clearing the event log
// Clears the event log for the current site
EventLogHelper.ClearEventLog(MembershipContext.AuthenticatedUser.UserID, MembershipContext.AuthenticatedUser.UserName, RequestContext.UserHostAddress, SiteContext.CurrentSiteID);