Event log
List of examples:
Dependency injection
Initialize required services
C#
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IEmailService emailService;
private readonly IEventLogService eventLogService;
private readonly IInfoProvider<EventLogInfo> eventLogInfoProvider;
private readonly IAuthenticatedUserAccessor authenticatedUserAccessor;
private readonly IHttpContextAccessor httpContextAccessor;
public EventLogServices(IEmailService emailService,
IEventLogService eventLogService,
IInfoProvider<EventLogInfo> eventLogInfoProvider,
IAuthenticatedUserAccessor authenticatedUserAccessor,
IHttpContextAccessor httpContextAccessor)
{
this.emailService = emailService;
this.eventLogService = eventLogService;
this.eventLogInfoProvider = eventLogInfoProvider;
this.authenticatedUserAccessor = authenticatedUserAccessor;
this.httpContextAccessor = httpContextAccessor;
}
Log events
C#
// Event log operations are performed using an instance of 'IEventLogService', typically obtained using dependency injection
// 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
eventLogService.LogInformation("API Example Info", "APIEXAMPLE", eventDescription: "Test information event.");
// Logs a warning event into the event log
eventLogService.LogWarning("API Example Warning", "APIEXAMPLE", eventDescription: "Test warning event.");
// Logs an error event into the event log
eventLogService.LogError("API Example Error", "APIEXAMPLE", eventDescription: "Test error event.");
// Logs an exception into the event log
eventLogService.LogException("API Example Exception", "APIEXAMPLE", new Exception(), additionalMessage: "Test exception event.");
Log events using LogEvent
C#
// Prepares an EventLogData object that holds all information about the event being logged
EventLogData eventData = new EventLogData(EventTypeEnum.Information, "API Example", "APIEXAMPLE")
{
EventTime = DateTime.Now,
EventDescription = "Logging a sample event to the event log."
};
// Event log operations are performed using an instance of 'IEventLogService', typically obtained using dependency injection
// Logs the event into the event log
eventLogService.LogEvent(eventData);
Work with events (sending events by email)
C#
// Gets all error events logged in the past day
var errors = eventLogInfoProvider.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 += $"<li>{errorEvent.EventType} - {errorEvent.EventCode} - {errorEvent.EventDescription}</li>";
}
msg.Body += "</ul></body></html>";
// Email sending is performed using an instance of 'IEmailService', typically obtained using dependency injection
// Sends out the email message
emailService.SendEmail(msg);
}
Clear the event log
C#
// Clears the event log for the current site
EventLogHelper.ClearEventLog(authenticatedUserAccessor.Get().Result.UserID, authenticatedUserAccessor.Get().Result.UserName, httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString());