编写日志消息并在Perfview中显示的最简单方法 [英] Simplest way to write a log message and display in Perfview

查看:56
本文介绍了编写日志消息并在Perfview中显示的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 PerfView 中写一条日志消息并将其捕获.我想避免使用 EventLog EventSource ,因为它们具有很高的侵入性:它们需要注册一个新的源或ETW提供程序,这会在系统中留下剩余的内容.

I need the to write a log message and capture that in PerfView. I would like to avoid using EventLog or EventSource because they are quite invasive: they require registering a new source or ETW provider, which leaves leftovers in the system.

理想情况下,我只想调用 Debug.WriteLine (它使用

Ideally I just want to call Debug.WriteLine (which uses OutputDebugString), but it seems that PerfView cannot collect it. Or is there some ETW provider that sees debug messages?

如果您有答案,请说明解决方案的两个部分:

If you have an answer, please state the two parts of the solution:

  1. 我应该用C#编写什么,并且
  2. 如何配置PerfView来捕获它(如果有一些ETW提供程序,只需将其命名).

推荐答案

您要使用的是

What you want is to use EventSource. Here you don't need to deal with GUIDs and registration to system.

public sealed class MinimalEventSource : EventSource
{
    public class Tasks
    {
        public const EventTask Information = (EventTask)1;
    }

    public static MinimalEventSource Log = new MinimalEventSource();

    [Event(1, Message = "{0}", Opcode = EventOpcode.Info, Task = Tasks.Information)]
    public void Information(string message)
    {
        if (IsEnabled())
        {
            WriteEvent(1, message);
        }
    }
}

使用 MinimalEventSource.Log.Information("my debug info"); 清除数据,并使用 PerfView/OnlyProviders = * MinimalEventSource 用perfview捕获数据.最重要的是 * .Eventsource通过 ManifestEvent 将带有定义的清单记录到ETL中,因此不需要清单注册.

Lof the data with MinimalEventSource.Log.Information("my debug info"); and capture them with perfview with PerfView /OnlyProviders=*MinimalEventSource. The imprtant thing is the * . Eventsource logs the Manifest with the definitions via ManifestEvent which gets added to the ETL, so no manifest registration is required.

这篇关于编写日志消息并在Perfview中显示的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆