定义 Source 后,C# 无法写入 EventLog

问题描述 投票:0回答:1

我正在尝试写入 Windows 的“EventLog”>“Windows 日志”>“应用程序”,我很确定我已经创建了所需的所有内容,但是在写入时,它只是抛出一个

Unknown error (0xe06d7363)
而没有给出任何信息。检查
EventSource
时,它确实报告源存在。

创建的注册表项,例如:

  • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MyApp
  • MyApp 下的
  • A
    ProviderGuid
  • Myapp下的
  • A
    EventMessageFile
    ,即指向
    %SystemRoot%\System32\mscoree.dll

尝试写入事件日志时,使用了以下代码片段:

if (!SourceExists)
{
    // When debugging, it never went in here because
    // I've already created the source in the registry
    if (!EventLog.SourceExists(Source))
    {
        EventLog.CreateEventSource(new EventSourceCreationData("MyApp", "Application"));
        SourceExists = true;
    }
}

using (EventLog eventLog = new EventLog())
{
    eventLog.Log = "Application";
    eventLog.Source = "MyApp";
    // the following line will throw Unknown error (0xe06d7363)
    eventLog.WriteEntry("Hello world", EventLogEntryType.Information, 1000);
}

我使用事件 id

1000
,因为开箱即用,
mscoree.dll
,具有默认格式,可以让您按原样转储字符串。

我还缺少什么?

PS:如果我也使用

Application
作为源,那么它会起作用,但我希望能够以
MyApp
的形式登录,而不是
Application

c# windows logging event-log
1个回答
0
投票

我设法让它工作,但事实证明我需要引入另外 2 个注册表项才能让它工作。我不确定这是否是 Windows 10/11 中的新功能,但我对其他一些 Microsoft 应用程序进行了逆向工程(例如:

Microsoft-Windows-RestartManager
下的
EventLog\Application
),这就是他们所做的。

您需要在

Publisher
下添加应用程序的 GUID。因此,在
EventLog\Application\MyApp
下,您有
ProviderGuid
,获取该 GUID 并在以下位置下添加一个新密钥:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers\{your-guid-value-goes-here}

您需要有一个

(Default)
值,后跟
MessageFileName

enter image description here

我使用 InnoSetup 来创建我的安装程序并创建必要的注册表项,这是我所拥有的:

[Registry]
Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers\{#MyGuid}"; ValueType: string; ValueData: "{#MyAppExeName}"; Flags: uninsdeletevalue
Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers\{#MyGuid}"; ValueType: string; ValueName: "MessageFileName"; ValueData: "%SystemRoot%\System32\mscoree.dll"; Flags: uninsdeletevalue
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Services\EventLog\Application\{#MyAppExeName}"; ValueType: expandsz; ValueName: "ProviderGuid"; ValueData: "{#MyGuid}"; Flags: uninsdeletevalue
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Services\EventLog\Application\{#MyAppExeName}"; ValueType: string; ValueName: "EventMessageFile"; ValueData: "%SystemRoot%\System32\mscoree.dll"; Flags: uninsdeletevalue

希望这对其他人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.