3

我正在使用应用程序事件日志来编写有关我的程序中发生的活动的消息。我将源设置为我的应用程序的名称。我想为用户提供仅清除与我的程序相关的事件的能力。这可能吗?我只看到一种清除整个日志的方法。

我在 .NET 2.0 中使用 c#。

4

2 回答 2

1

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

于 2013-12-21T06:41:51.517 回答
1

无法从事件日志中清除特定事件。很清楚所有事件或什么都没有。

于 2010-02-23T14:26:34.977 回答