简体   繁体   English

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

[英]Windows Event Log - how to register an event source?

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? 我究竟做错了什么?

There's the following note in MSDN: 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? 是否有可能在尝试您之前尝试写入应用程序日志的代码时,现在需要重新启动才能“取消映射”该链接?

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. 我决定做的是检查源是否存在,是否检查它是否链接到正确的日志,如果它不是删除源,现在它不存在或者它从未创建过Log new 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 :) 希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM