我可以列出所有注册的活动来源吗?

Kas*_*sen 12 c# event-log

我的Windows服务写入事件日志,但我遇到了各种问题,这是正确的.所以在这个过程中我使用了许多不同的名字.我跟着一篇文章描述了如何在Windows服务中设置事件日志.因此,在设计器中添加EventLog组件后,我已将其添加到构造函数中:

if (!System.Diagnostics.EventLog.SourceExists("AS0604"))
   System.Diagnostics.EventLog.CreateEventSource("AS0604", "SIRR");

eventLog1.Source = "AS0604";
eventLog1.Log = "SIRR";
eventLog1.WriteEntry("AS is initializing...", EventLogEntryType.Information, 16);
Run Code Online (Sandbox Code Playgroud)

我发现,如果源名称与Windows服务的服务名称相同,则会出现问题.但是我一直在为Log和Source更改名称.该

EventLog[] eventLogs = EventLog.GetEventLogs();
Run Code Online (Sandbox Code Playgroud)

列出事件日志,我能够删除那些我没有使用EventLog.Delete命令.

但这是如何工作的?这些已删除的日志中是否仍有已注册的来源?我可以获得注册来源列表吗?

Tom*_*ter 6

从它的一些好处开始,看起来答案是你无法从这个API获得一个源列表.删除日志会删除在其中注册的源.

此页面告诉您如何使用直接注册表访问来执行此操作:

http://codeidol.com/csharp/csharpckbk2/Diagnostics/Finding-All-Sources-Belonging-to-a-Specific-Event-Log/


Sco*_*ker 5

由于接受的答案丢失了,这里是另一个。不幸的是,我发现除了直接检查 Windows 注册表之外别无选择。

  • PowerShell (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\<EventLogPathName>).pschildname

例如,列出 Windows应用程序事件日志的来源:

  • PowerShell (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application).pschildname

在阅读了几个来源后,我抛出了这个问题。不幸的是,没有一个是非常清楚或直接的。


tim*_*B33 1

通过powershell:

Get-EventLog -LogName Application |Select-Object Source -Unique 
Run Code Online (Sandbox Code Playgroud)

请参阅: https: //social.technet.microsoft.com/Forums/windowsserver/en-US/48d1e34d-6ded-4039-a8a4-3b17d9c69488/list-eventlog-sources ?forum=winserverpowershell

  • 这并不显示所有已注册的来源。如果当前日志中没有给定源的条目,则此处不会显示该条目。 (3认同)