Windows 事件日志 - 如何注册事件源?

发布于 2024-08-30 03:05:27 字数 535 浏览 4 评论 0原文

我正在创建一个新的事件源并使用以下代码记录一条消息:

    static void Main(string[] args)
    {
        if (!EventLog.SourceExists("My Log"))
        {
            EventLog.CreateEventSource("My Application", "My Log");
            Console.WriteLine("Created new log \"My Log\"");
        }

        EventLog myLog = new EventLog("My Log");
        myLog.Source = "My Application";
        myLog.WriteEntry("Could not connect", EventLogEntryType.Error, 1001, 1);
    }

创建了一个名为“我的日志”的自定义事件日志(如预期),但该消息记录在“应用程序”节点下方。我做错了什么?

I am creating a new event source and logging a message using the code below:

    static void Main(string[] args)
    {
        if (!EventLog.SourceExists("My Log"))
        {
            EventLog.CreateEventSource("My Application", "My Log");
            Console.WriteLine("Created new log \"My Log\"");
        }

        EventLog myLog = new EventLog("My Log");
        myLog.Source = "My Application";
        myLog.WriteEntry("Could not connect", EventLogEntryType.Error, 1001, 1);
    }

A custom event log with the name "My Log" is created (as expected) but the message is logged below the "Application" node. What am I doing wrong?

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

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

发布评论

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

评论(2

梦幻的味道 2024-09-06 03:05:27

MSDN 中有如下注释:

如果源已映射到日志并且您将其重新映射到新日志,则必须重新启动计算机才能使更改生效。

是否有可能在尝试您之前尝试写入应用程序日志的代码时,现在需要重新启动才能“取消映射”该链接?

There's the following note in MSDN:

If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

Is it possible while trying out the code that you previously tried writing to the Application log and you now need to reboot for it to "unmap" that link?

梦醒灬来后我 2024-09-06 03:05:27

我认为你似乎在某个地方混淆了事情。

您有一个源(即您的应用程序)并且该源链接到日志,这是在创建源时完成的
您在代码的开头将这些内容混淆了一点,实际上应该是

    if (!EventLog.SourceExists("My Application"))

我刚刚编写了一些代码来帮助我解决这个问题。源在我遇到的另一个日志问题中注册,并且不想手动从日志中删除源。
我决定做的是检查源是否存在,是否检查其链接到正确的日志,如果不删除源,现在它不存在,或者它从未创建全新的日志。

protected const string EventLogName = "MyLog";

private static bool CheckSourceExists(string source) {
  if (EventLog.SourceExists(source)) {
    EventLog evLog = new EventLog {Source = source};
    if (evLog.Log != EventLogName) {
      EventLog.DeleteEventSource(source);
    }
  }

  if (!EventLog.SourceExists(source)) {
    EventLog.CreateEventSource(source, EventLogName);
    EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
  }

  return EventLog.SourceExists(source);
}

public static void WriteEventToMyLog(string source, string text, EventLogEntryType type) {      
  if (CheckSourceExists(source)) {          
      EventLog.WriteEntry(source, text, type);          
  }
}

希望它有帮助:)

You appear to have things mixed up somewhere there I think.

You have a source (which is your application) and that source is linked to a Log, this is done when you Create your source
You have mixed these up a little bit at the beginning of your code, it should in fact be

    if (!EventLog.SourceExists("My Application"))

I have just written a little code to help me out of this. source registered in another log issue which I have encountered and don't want to manually have to remove sources from logs.
What I decided to do was check if the source exists, if it does check that its linked to the correct log, if it isn't delete the source, now that it doesn't exist or f it never did create the Log brand new.

protected const string EventLogName = "MyLog";

private static bool CheckSourceExists(string source) {
  if (EventLog.SourceExists(source)) {
    EventLog evLog = new EventLog {Source = source};
    if (evLog.Log != EventLogName) {
      EventLog.DeleteEventSource(source);
    }
  }

  if (!EventLog.SourceExists(source)) {
    EventLog.CreateEventSource(source, EventLogName);
    EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
  }

  return EventLog.SourceExists(source);
}

public static void WriteEventToMyLog(string source, string text, EventLogEntryType type) {      
  if (CheckSourceExists(source)) {          
      EventLog.WriteEntry(source, text, type);          
  }
}

Hopefully it helps :)

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