【问题标题】:EventSource .net 4.0 GenerateManifestEventSource .net 4.0 生成清单
【发布时间】:2013-09-20 09:33:03
【问题描述】:

我一直在尝试在 .net 4.0 中使用 ETW。

我已经开始使用 Microsoft EventSource Library 1.0.4-beta (https://www.nuget.org/packages/Microsoft.Diagnostics.Tracing.EventSource)

这是我为我的应用程序生成事件而编写的代码。

[EventSource(Name = "Samples-EventSourceDemos-EventSourceLogger")]
public sealed class EventSourceLogger : EventSource
{
    public static EventSourceLogger Log = new EventSourceLogger();

    public static string GetManifest()
    {
        return GenerateManifest(typeof(EventSourceLogger), null);
    }

    [Event(200, Level = Microsoft.Diagnostics.Tracing.EventLevel.Informational, Task = EventTask.None, Version = 1,
        Opcode = EventOpcode.Info, Keywords = EventKeywords.None, Channel = EventChannel.Admin,
        Message = "Test Message")]
    public void LogEtwInfoEventMessage(string jsonArgs)
    {
        if (!this.IsEnabled()) return;

        this.WriteEvent(200, jsonArgs);
    }

    [Event(400, Level = Microsoft.Diagnostics.Tracing.EventLevel.Error, Task = EventTask.None, Version = 1,
        Opcode = EventOpcode.Info, Keywords = EventKeywords.None, Channel = EventChannel.Admin, Message = "Test Message")]
    public void LogEtwErrorEventMessage(string jsonArgs)
    {
        if (!this.IsEnabled()) return;

        this.WriteEvent(400, jsonArgs);
    }

    [Event(500, Level = Microsoft.Diagnostics.Tracing.EventLevel.Warning, Task = EventTask.None, Version = 1,
       Opcode = EventOpcode.Info, Keywords = EventKeywords.None, Channel = EventChannel.Admin, Message = "Test Message")]
    public void LogEtwWarningEventMessage(string jsonArgs)
    {
        if (!this.IsEnabled()) return;

        this.WriteEvent(500, jsonArgs);
    }
}

我无法从侦听器生成清单。代码如下

var manifestXml = EventSourceLogger.GetManifest();

当我尝试调用它时,我得到 NullReferenceException,请建议我遗漏任何东西。是否可以使用此版本将 EventMessage 推送到 EventViewer。

作为此 NuGet 包的一部分,我有 eventRegister、Install Bat、Microsoft.Diagnostics.Tracing.EventSource.targets。我不确定这些对清单生成有何帮助。

如果有人对此有任何想法(或)工作,请帮助。

提前致谢。

【问题讨论】:

    标签: .net-4.0 event-log system.diagnostics etw etw-eventsource


    【解决方案1】:

    @magicandre1981 的回答是正确的,因为没有必要使用较新版本的 .NETEventSource 生成清单。 (事实上​​,它仍在发生,但它只是隐藏在一个构建事件后面,当您安装 EventSource package 时,该构建事件会被放入您的项目文件中。)

    但是,根据您的操作,您可能仍需要手动生成清单。这是一种方法:

    1. 要么将EventSource package (Install-Package Microsoft.Diagnostics.Tracing.EventSource) 安装到你的项目中,要么在你需要的地方下载并解压
    2. 找到eventRegister.exe。 (相对于软件包安装文件夹,它很可能位于类似于 packages\Microsoft.Diagnostics.Tracing.EventRegister.1.1.26\build 的文件夹下)
    3. 运行以下命令:

    eventRegister.exe {path-to-dll-with-your-eventsource-class} {manifest-output-file-prefix}

    之后,您将看到 dll 中每个 EventSource 类的两个文件:

    • {prefix}{EventSource Name}.etwManifest.dll
    • {prefix}{EventSource Name}.etwManifest.man

    然后您可以将这些内容发送给wevtutil

    wevtutil.exe 
       im {EtwManifestManFile} 
       /rf:"{EtwManifestDllFile}" 
       /mf:"{EtwManifestDllFile}"
    

    【讨论】:

    【解决方案2】:

    您不再需要获取清单。现在可以直接注册EventSource了:

    注册您的 EventSource

    安装 EventSource NuGet 包时,构建步骤 前面提到的为每个生成以下文件 应用程序中的事件源:

    AssemblyName.EventSourceTypeName.etwManifest.man

    AssemblyName.EventSourceTypeName.etwManifest.dll。

    这些文件需要在操作系统中注册才能启用 渠道支持。为此,您在 文件位于其最终部署位置:

    wevtutil.exe im EtwManifestManFile /rf:"EtwManifestDllFile"c /mf:"EtwManifestDllFile"

    Microsoft 在此博客中对此进行了解释:

    Announcing the EventSource NuGet Package – Write to the Windows Event Log

    【讨论】:

    • 我没有看到 AssemblyName.EventSourceTypeName.etwManifest.man 和 AssemblyName.EventSourceTypeName.etwManifest.dll 生成。你能告诉我清单是如何生成的吗?
    • eventRegister.exe 是否正确执行。也可以在微软博客中问这个,这个版本只是一个测试版,所以可能还有问题。
    • 事实证明,如果您希望使用管理通道写入 EventViewer(需要注册清单和 dll),您仍然需要生成清单。如果您知道更好的方法,请告诉我。
    【解决方案3】:

    我能够找到解决方案。现在我可以注册事件并将其发布到事件查看器。

    http://naveensrinivasan.com/2010/03/17/using-clr-4-0-event-tracing-for-windows-etw-along-with-application-etw/

    谢谢。

    【讨论】:

    • 我仍然不知道为什么它对你来说失败了。创建一个新的 C# 控制台应用程序并添加以下内容:Install-Package Microsoft.Diagnostics.Tracing.EventSource.Samples -Pre 我看到文件 DemoEventSource.Samples-EventSourceDemos-EventLog.etwManifest.man 在调试文件夹中。
    猜你喜欢
    • 2015-12-10
    • 2011-07-10
    • 2012-10-07
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多