Windows 事件日志 - 如何注册事件源?
我正在创建一个新的事件源并使用以下代码记录一条消息:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MSDN 中有如下注释:
是否有可能在尝试您之前尝试写入应用程序日志的代码时,现在需要重新启动才能“取消映射”该链接?
There's the following note in MSDN:
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
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.
Hopefully it helps :)