C# EventLog.CreateEventSource 未创建自定义日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1901312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EventLog.CreateEventSource is not creating a custom log
提问by Jez
I have some code like this:
我有一些这样的代码:
EventLog.CreateEventSource("myApp", "myAppLog");
EventLog.WriteEntry("myApp", "Test log message", EventLogEntryType.Error);
Now, unless I'm missing something having read MSDN, this should cause a new log 'myAppLog' to be created in the event viewer, and an entry should be added to that new log with the source name 'myApp'. But, I can't get the new log to be created. This always just writes an error log message to the Application log, with the source 'myApp' - 'myAppLog' is nowhere to be seen. What am I doing wrong? I am logged in as an Administrator.
现在,除非我错过了阅读 MSDN 的内容,否则这应该会导致在事件查看器中创建一个新日志“myAppLog”,并且应该将一个条目添加到该新日志中,源名称为“myApp”。但是,我无法创建新日志。这始终只是将错误日志消息写入应用程序日志,源“myApp”-“myAppLog”无处可见。我究竟做错了什么?我以管理员身份登录。
采纳答案by Chris Haas
Is it possible that you already used the source "myApp" when writing to the standard Application log? If so according to MSDN:
在写入标准应用程序日志时,您是否可能已经使用了源“myApp”?如果是这样,根据 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.
如果源已映射到日志,而您将其重新映射到新日志,则必须重新启动计算机以使更改生效。
http://msdn.microsoft.com/en-us/library/2awhba7a.aspx(about half way down the page)
http://msdn.microsoft.com/en-us/library/2awhba7a.aspx(大约页面的一半)
回答by David Basarab
Did you set the source on your EventLog?
您是否在 EventLog 上设置了来源?
From MSDN Article.
来自MSDN 文章。
You must set the Source property on your EventLog component instance before you can write entries to a log. When your component writes an entry, the system automatically checks to see if the source you specified is registered with the event log to which the component is writing, and calls CreateEventSource if needed. In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources.
您必须先在 EventLog 组件实例上设置 Source 属性,然后才能将条目写入日志。当您的组件写入条目时,系统会自动检查您指定的源是否已在组件写入的事件日志中注册,并在需要时调用 CreateEventSource。通常,在安装应用程序期间创建新的事件源。这允许操作系统有时间刷新其注册事件源列表及其配置。如果操作系统尚未刷新其事件源列表,而您尝试使用新源写入事件,则写入操作将失败。如果在安装期间创建源不是一个选项,那么尝试在第一次写入操作之前创建源,可能是在您的应用程序初始化期间。如果您选择这种方法,请确保您的初始化代码在计算机上以管理员权限运行。创建新事件源需要这些权限。
回答by Joseph
You might be forgetting to set the Source property on your EventLog.
您可能忘记在 EventLog 上设置 Source 属性。
It should look something like this:
它应该是这样的:
if(!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
EventLog myLog = new EventLog();
myLog.Source = "MySource";
myLog.WriteEntry("Writing to event log.");
Here's the MSDN articlefor reference.
这是MSDN 文章供参考。
回答by PJUK
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 :)
希望它有帮助:)
回答by LCJ
If you have checked all suggestions in other answers, then read the following
如果您已检查其他答案中的所有建议,请阅读以下内容
From MSDN
来自MSDN
The operating system stores event logs as files. When you use EventLogInstaller or CreateEventSource to create a new event log, the associated file is stored in the %SystemRoot%\System32\Config directory on the specified computer. The file name is set by appending the first 8 characters of the Log property with the ".evt" file name extension.
操作系统将事件日志存储为文件。当您使用 EventLogInstaller 或 CreateEventSource 创建新的事件日志时,关联的文件存储在指定计算机上的 %SystemRoot%\System32\Config 目录中。通过在 Log 属性的前 8 个字符后附加“.evt”文件扩展名来设置文件名。
Make sure that the first 8 charactersare unique.
确保前 8 个字符是唯一的。
回答by Himanshu Soni
Basically, the simplest solution is you have to close your visual studio and run as administrator mode. Then, you would be able to resolve this error
基本上,最简单的解决方案是您必须关闭 Visual Studio 并以管理员模式运行。然后,您将能够解决此错误