How to exclude specific user from the Event Log
This article provides an example of excluding (deleting) events of a specific user from the Event log.
Unfortunately, there is no way to exclude a user from creating an event log. So if you want to delete the specific user (log item) from the Event log, the only workaround is to set a scheduling task which will run every X minutes and delete selected events. It’s also great if you are using a third party service, for example, that is periodically logging into your site, which could create a huge event log. It would be better to have the Event log cleared of these events, especially for readability.
public string Execute(TaskInfo ti)
{
// Create new instance of event log provider
EventLogProvider eventLog = new EventLogProvider();
string[] splitUsers = ti.TaskData.Split(';');
foreach (string user in splitUsers)
{
// Get events matching the where condition
string where = "UserName = '" + user + "'";
DataSet events = eventLog.GetAllEvents(where, null);
if (!DataHelper.DataSourceIsEmpty(events))
{
int cnt = 0;
// Loop through the individual items
foreach (DataRow eventDr in events.Tables[0].Rows)
{
// Create the object from DataRow
EventLogInfo deleteEvent = new EventLogInfo(eventDr);
//Delete log
deleteEvent.Destroy();
eventLog.SetEventLogInfo(deleteEvent);
cnt++;
}
string detail = cnt.ToString() + " events of user '" + user + "' deleted from Event Log.";
// Logs the execution of the task in the event log.
EventLogProvider.LogInformation("DeleteUsers", "Execute", detail);
}
}
return null;
}
Note: Add user names delimited by ';' to the Task Data property of your Scheduled Task.
Special thanks go to Vlado.
-md-
See also: Scheduler - Configuring task execution
Managing log events
Applies to: Kentico CMS 6.x, 7.x