Simplest way to write a log message and display in Perfview

柔情痞子 提交于 2019-12-20 06:25:12

问题


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.

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. What should I write in C#, and
  2. How to configure PerfView to capture it (if there is some ETW provider, just name it).

回答1:


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);
        }
    }
}

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.



来源:https://stackoverflow.com/questions/54070364/simplest-way-to-write-a-log-message-and-display-in-perfview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!