5

I understand that using Perfmon.msc you can create a custom performance counter and by using counter log, you can write the counter value to a text file.

I also understand I can also use this programmatically by creating a performance counter by using System.Diagnostics.PerformanceCounter, and get the counter value using NextValue() method. Is there a programmatical way to tell the PerformanceCounter object to write the log to a text file too (similar to Counter Log in perfmon.msc)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

2 Answers2

4

In .NET you will have to sample the counter yourself and write the sampled value to a file. However, the Win32 API has functions to do what you want. For an example of the native API see Writing Performance Data to a Log File. You could try to use p/invoke to call the API.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • @Louis Rhys: Only if you use platform invoke (p/invoke) to call the API from managed code or create a managed C++ wrapper around the API. P/invoke is probably easier, but some API's are really hard to call in that way. I havn't tried the PDH API myself. – Martin Liversage Aug 25 '10 at 08:11
2

Using P/Invoke as suggested by another poster gave too much trouble. I suggest running logman from command line using System.Diagnostics.Process.Start()

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221