Reputation:
I'm using the application event log to write messages about activity happening in my program. I set the source to the name of my app. I would like to offer the user the ability to clear just the events related to my program. Is this possible? I only see a way to clear the whole log.
I'm using c# in .NET 2.0.
Upvotes: 3
Views: 2066
Reputation: 333
This is a piece of code from MSDN library see if this is what your looking for.
string logName;
if (EventLog.SourceExists("MySource"))
{
// Find the log associated with this source.
logName = EventLog.LogNameFromSourceName("MySource", ".");
// Make sure the source is in the log we believe it to be in.
if (logName != "MyLog")
return;
// Delete the source and the log.
EventLog.DeleteEventSource("MySource");
EventLog.Delete(logName);
Console.WriteLine(logName + " deleted.");
}
else
{
// Create the event source to make next try successful.
EventLog.CreateEventSource("MySource", "MyLog");
}
Also what worked for me was creating an Event Source for my application using
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, additional);
}
and logging all the details in there using
EventLog.WriteEntry(Source, error, EventLogEntryType.Error);
and then when i want to clear my logs i use
EventLog myEventLog = new EventLog(Constants.EventSource);
myEventLog.Clear();
This will clear only the logs you've logged under your source. Hope it helps :)
Upvotes: 1
Reputation: 5412
It is not possible to clear specific events from an Event log. It's clear all events or nothing.
Upvotes: 1