如何仅从一个来源的应用程序事件日志中删除所有事件?

发布于 2024-08-23 08:26:36 字数 126 浏览 13 评论 0原文

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

我在 .NET 2.0 中使用 C#。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-08-30 08:26:36

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

It is not possible to clear specific events from an Event log. It's clear all events or nothing.

喜爱纠缠 2024-08-30 08:26:36

这是来自 MSDN 库的一段代码,看看这是否是您想要的。

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

另外,对我有用的是使用

 if (!EventLog.SourceExists(source))
        {
            EventLog.CreateEventSource(source, additional);
        }

并记录其中的所有详细信息

 EventLog.WriteEntry(Source, error, EventLogEntryType.Error);

为我的应用程序创建一个事件源,然后当我想清除我的日志时,我使用

 EventLog myEventLog = new EventLog(Constants.EventSource);
 myEventLog.Clear();

这将仅清除您在源下记录的日志。希望它有帮助:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文