简体   繁体   中英

Performance Counter : “Database Cache % Hit”

I used this bit of code to retrieve the available performance counters on the machine I want to monitor:

        var allCounters = new List<PerformanceCounter>();
        foreach (var category in PerformanceCounterCategory.GetCategories("machine-name"))
        {
            var names = category.GetInstanceNames();
            if (names.Length > 0)
            {
                foreach (var name in names)
                {
                    allCounters.AddRange(category.GetCounters(name));
                }
            }
            else
            {
                allCounters.AddRange(category.GetCounters());
            }
        }

I found a counter which I want to monitor: Database Cache % Hit . Its does not have any instance names so I just get it this way:

new PerformanceCounter("Database", "Database Cache % Hit", null, "machine-name"); //null or "" for the third argument

This works until I call the NextValue method on it, which raises an InvalidOperationException :

Counter is not single instance, an instance name needs to be specified.

I tried putting the name of the SQL Server DB Name I want to monitor but it does not work either (it fails during instanciation instead).

How can I use this performance counter in my C# application?

Unfortunately, "Database" counters group has nothing to do with SQL server. It is part of ESENT infrastructure.

For SQL server statistics look for MSSQL$instance_name counters. Probably you are looking for Buffer Manager cache hit ratio, however there are other caches (like query plan cache).

Also you may want to query statistics directly from sys.dm_os_performance_counters view, bypassing this excessive windows counters layer...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM