Simplest way to write a log message and display in

2020-05-01 08:35发布

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条回答
Juvenile、少年°
2楼-- · 2020-05-01 09:14

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.

查看更多
登录 后发表回答