Adding a counter through PerformanceCounterCategory
Hi Once I create a Performance Counter Category using PerformanceCounterCategory.Create, is it possible to add an additional counter to that category? Or do I have to first delete it and re-create it?
T开发者_开发百科hanks Saar
In all my code, I always either create all counters, or delete them, but never modify them to add just one. Its not typically the thing to do as perf counters are usually installed once as part of the install of an app, or deleted once the app is removed. Again, I normally have an installer class so I can run InstallUtil over it and have the counters added or removed.
To answer your question, I dont think you can individually manipulate them. The PerformanceCounterCategory class seems to allow creation and deletion, and also the ability to get the associated counters, but not modify them and re-apply.
I think this would be a bad approach anyway as it places extra burden in the code to manage this. Just uninstall and re-install when needed and having an installer class makes this really easy.
I ran into this post in digging for an answer to a related issue I was having with performance counters, so I thought I'd update here. From what I read on this MSDN page, you cannot alter an existing category, you have to delete and recreate.
...you must create a counter in a new category; adding a counter to an existing user-defined category will raise an exception.
Answer https://stackoverflow.com/a/4742793/52277 to the question How to add a new counter to an existing performance counter category without deleting the old counters? describes how to preserve counters before deleting and re-creating category
精彩评论