---
title: Event log
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

### Logging events

```csharp

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.");

```

[> Back to list of examples](#toc)

### Logging events using LogEvent

```csharp

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);

```

[> Back to list of examples](#toc)

### Working with events (sending events by email)

```csharp

// 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);
}

```

[> Back to list of examples](#toc)

### Clearing the event log

```csharp

// Clears the event log for the current site
EventLogHelper.ClearEventLog(MembershipContext.AuthenticatedUser.UserID, MembershipContext.AuthenticatedUser.UserName, RequestContext.UserHostAddress, SiteContext.CurrentSiteID);

```

[> Back to list of examples](#toc)
