我的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);
我发现如果源名称与Windows服务的服务名称相同,则会出现问题。但是我一直在为Log和Source更改名称。
EventLog[] eventLogs = EventLog.GetEventLogs();
列出事件日志,我能够删除那些我没有使用EventLog.Delete命令。
但这是如何运作的?这些已删除的日志中是否仍有已注册的来源?我可以获得注册来源列表吗?
答案 0 :(得分:7)
从一点点开始玩它,看起来答案是你无法从这个API获得一个源列表。删除日志会删除在其中注册的源。
此页面告诉您如何使用直接注册表访问来执行此操作:
答案 1 :(得分:0)
由于接受的答案丢失了,这里是另一个。不幸的是,我没有找到直接检查Windows注册表的替代方法。
PowerShell (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\<EventLogPathName>).pschildname
例如列出Windows 应用程序事件日志的来源:
PowerShell (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application).pschildname
在阅读了多个资料后,我将其提出来。不幸的是,没有一个是非常明确或直接的。
答案 2 :(得分:0)
我没有C#答案,但这是WMI解决方案:
$Sources = Get-WmiObject -Namespace "root\cimv2" -Class "Win32_NTEventLOgFile" | Select-Object FileName, Sources | ForEach-Object -Begin { $hash = @{}} -Process { $hash[$_.FileName] = $_.Sources } -end { $Hash }
即使给定源的日志中当前没有条目,这也会列出源。
答案 3 :(得分:-1)
通过powershell:
Get-EventLog -LogName Application |Select-Object Source -Unique