I want to retrieve performance Counters from a W3WP (IIS 10.0) Process. I', able to read Process and Thread Performance counters, but not any other like % Time spent in GC
So I wrote a small console showing me all Categories which may have a associations with w3wp and I only see Process and Thread.
I also ran the console as administrator and as the user with which the app pool is running. But still same result.
What am I missing?
var cats = PerformanceCounterCategory.GetCategories();
foreach (var cat in cats)
{
var names = cat.GetInstanceNames();
if (names.Any(i => i.ToLowerInvariant().Contains("w3wp")))
{
Console.WriteLine(cat.CategoryName);
}
}
private static string GetProcessInstanceName(int pid)
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
string[] instances = cat.GetInstanceNames();
foreach (string instance in instances)
{
using (PerformanceCounter cnt = new PerformanceCounter("Process",
"ID Process", instance, true))
{
int val = (int)cnt.RawValue;
if (val == pid)
{
return instance;
}
}
}
}
You have to run the application in the same bitness as your website. If your website is 64 bit then you need to run the application in 64 bit mode.
To do this right click on the console application project, click properties in the Build tab untick the box that says "Prefer 32-bit".
See Reading w3wp .Net Performance Counter Instances programmatically for more info.
Related
So I have an IIS10 webserver, running a quite a few applications, like:
+ Default Web Site
-First Application
-Second Application
-Third Application
We are using nLog to log stuff to a database, one of which is the current appdomain
${appdomain:format=Format}
as documented at https://github.com/NLog/NLog/wiki/AppDomain-Layout-Renderer.
This is great, except when I look at our consolidated logs, I see that the appdomain is logged as:
0002:/LM/W3SVC/2/ROOT-1-132036049222959352
I want to translate that into "Third Application" (So when I create a report, it will show up as "Third Application" instead of 0002:/LM/W3SVC/2/ROOT-1-132036049222959352.)
Conditions are:
I can't change the layoutrenderer.
I can't change the code of the applications.
I have to do this AFTER the data is logged. I assume that /LM/W3SVC/2/ROOT is what I need to transform, and I can't do it in the app, only in reporting.
I've tried Microsoft.Web.Administration, something like:
using (ServerManager sm = new ServerManager()){
var thisVal = (from s in sm.Sites
from app in s.Applications
from vDir in app.VirtualDirectories
where vDir.Path.Equals(currVal,StringComparison.InvariantCultureIgnoreCase)
select new KeyValuePair<string, string>(currVal, vDir.Path));
}
But I can't find the app root in there anywhere. I believe it's the same thing as https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524308(v=vs.90), but that's the IIS metabase, which is apparently old and busted.
(the code above will be running on the same machine as the applications, so I don't have to worry about finding the correct server, just translating the name)
You can enumerate (and thus filter for) the AppDomain's id as such:
using (ServerManager s = new ServerManager())
{
var processList = from m in Process.GetProcessesByName("w3wp") select m.Id;
IEnumerable<WorkerProcess> workerP = (from p in s.ApplicationPools
from w in p.WorkerProcesses
where processList.Any(vn => vn == w.ProcessId)
select w);
foreach (var workProcess in workerP)
{
Console.WriteLine(
$"{workProcess.AppPoolName} Id:{workProcess.ProcessId} AppDomains Count: {workProcess.ApplicationDomains?.Count}");
var ad = workProcess.ApplicationDomains;
if (!ad.Any()) continue;
foreach (var curDomain in ad)
{
Console.WriteLine($"AppDomain: {curDomain.Id} {curDomain.VirtualPath}");
}
}
}
This will give you output that shows the AppDomain's ids, that you can get the vdir from:
DefaultAppPool Id:33180 AppDomains Count: 1
AppDomain: /LM/W3SVC/1/ROOT/
I am connecting to various performance counters in the Process category. I am using the following c# method to determine the instance name to use when acquiring the counters:
private const string _categoryName = "Process";
private const string _processIdCounter = "ID Process";
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
instanceName = null;
return false;
}
Now, I have noticed that the instance name returned usually matches the value of Process.ProcessName.
How are the instance name and process name related?
I ask because I want to simplify the foreach loop in the routine so that I do not have to acquire the ID Process counter for instances that cannot match the current process. I envision a final method that might look like this:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
if (name /* more or less matches */ process.ProcessName)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
// ...
}
}
}
instanceName = null;
return false;
}
Seeing that an answer was not forthcoming, I did some more trial and error testing and observed the following behaviour:
Regular Processes
It appears that, for the first regular process with a given name, the process name matches the instance name. For subsequent processes with the same name, the instance name is modified by appending #1, #2, ...
Alarmingly, it also appears to be possible for the instance name associated with the process to change. This appears to happen when processes earlier in the numeric sequence end. There is a race-condition between determining the instance name and acquiring the relevant counters!
Service Processes
Windows NT Services running under the Service Control Manager appear to behave in the same way that regular processes behave. The instance name also appears to change if you end a service-process earlier in the numeric sequence.
ASP.NET Applications
The same assumptions work for applications hosted under IIS except that the process name is w3wp. Different app. pools definitely get different processes and, by starting and stopping app. pools, I ascertained that the instance name changes in the same way, under the same circumstances as above.
Conclusion
My conclusion is that the instance name always starts with the process name and the method can be modified as follows:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
if (name.StartsWith(process.ProcessName))
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
}
instanceName = null;
return false;
}
Additionally, it is vitally important that one acknowledges the presence of the race-condition mentioned above when using the instance name returned.
(In the absence of further input, I will accept this as an answer. Feel free to correct me.)
Also note that if you are monitoring muliple instances of the same process then the instance naming isn't consistent across different categories. So the solution given above only works if you are pulling counters from the same category that the pid was was pulled from. I did find the pid was in some other categores - but not all - and not with consistent naming.
Wouldn't this solution be a little bit faster:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string processName = Path.GetFileNameWithoutExtension(process.ProcessName);
string[] instanceNames = processCategory.GetInstanceNames()
.Where(inst => inst.StartsWith(processName))
.ToArray();
foreach (string name in instanceNames)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
instanceName = null;
return false;
}
(Copied from Rick Strahl's blog and modified a bit).
Nevertheless, you need to take care: If there a multiple processes with the same name and one of them exits, the naming of all of them changes:
One thing to mention related to windows process instance names is that they change dynamically when one of the processes exits.
For example if chrome#8 exits, chrome#9 will become chrome#8 and chrome#10 >will become chrome#9. At this point getting the value of the counter >previously created for chrome#10 will throw an exception. This
is really annoying if you want to to monitor multiple instances of
multiple processes as it gets down to monitoring process exits and
recreating all the counters (really ugly).
One way would be to change the way process instance names are generated >(see http://support.microsoft.com/kb/281884) but this has
the potential of affecting other apps using the perfmon api.
I create one Windows Form App on my friends machine. It works fine on his machine. But when I tried to run same App on my own machine then from _dialog.ShowDialog() line an exception thrown like "accessviolationexception attempted to read or write protected memory. this is often an indication that other memory is corrupt...". I check for this error on net and I found following solutions:
1) Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load" link : http://social.msdn.microsoft.com/Forums/en-US/8789ea67-fbc5-4a7b-a4eb-d4a8a050d5c1/attempt-to-read-or-write-protected-memory-this-is-often-an-indicating-that-other-memory-is-corrupt. Done on my machine but wasnt work.
2) Attempted to read or write protected memory, Install http://support.microsoft.com/kb/971030 for framework 2.0,..3.5, but I dont find any of download product from mention link.
My machine configuration: VS 2010(SP1), Framework used 4.0, DB used MS-Access.
Block of code:
private void SetAttachmentInfo()
{
Dictionary<string, object> _fileInfo = new Dictionary<string, object>();
OpenFileDialog _dialog = new OpenFileDialog();
var _fileName = (object)(null);
var _fileData = (object)(null);
var _fileDataLength = (object)(null);
_dialog.Multiselect = false;
_dialog.Filter = "Office Files (*.doc;*.xls;*.ppt;*pdf;*txt) |*.doc;*xlsx;*.xls*.ppt;*pdf;*.txt;|Image Files (*.jpeg;*.png;*.jpg;*.gif) |*.jpeg;*.png;*.jpg;*.gif |All File|*.*";
if (_dialog.ShowDialog() != DialogResult.Cancel)
{
_fileInfo = GetAttachmentFileInformation(_dialog.FileName);
_fileInfo.TryGetValue("FileName", out _fileName);
_fileInfo.TryGetValue("FileData", out _fileData);
_fileInfo.TryGetValue("Lenght", out _fileDataLength);
FileName = Convert.ToString(_fileName);
FileData = (_fileData != null && (_fileDataLength as int?) > 0) ? (byte[])_fileData : (byte[])null;
AttachmentLength = _fileDataLength as int?;
}
}
Any useful help?
Turning off the DEP settings may solve your problem. Turn off DEP via an elevated Command Prompt by clicking the Windows (Start) > All Programs > Accessories and right-click Command Prompt, then ‘Run as Administrator’. Type bcdedit.exe /set {current} nx AlwaysOff (note the four spaces) and press Enter. To turn it back on, change AlwaysOff to AlwaysOn. You need to restart the system after the changes have been made.
I am trying to prevent opening help file more than once.
This is the method I am using:
public void openHelp()
{
int count = 0;
string helpPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + #"\MyApp\Help\eHelp.chm";
System.Diagnostics.Process[] helpProcs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process proc in helpProcs)
{
if (proc.MainWindowTitle == "Sample App Help")
{
count++;
}
}
if (count == 0)
{
System.Diagnostics.Process.Start(helpPath);
}
else
{
}
}
The idea is, if you find the process with the same MainWindowTitle, then do not start a new one.
However, this is not reliable. In some cases it still starts the process, even though one is already running. Is there an issue with a logic?
Thank you.
P.S. Of course the MainWindowTitle is "Sample App Help", at least that is what I see while debugging.
Update:
Issue only occurs when user has minimised help file. So I suspect something happens in the system and I need to check something. Any suggestions?
The Remarks section in Process.MainWindowTitle contains the following note:
The main window is the window that currently has the focus; note that
this might not be the primary window for the process. You must use the
Refresh method to refresh the Process object to get the current main
window handle if it has changed.
Could this perhaps be the cause of your problem?
What about keeping the process id of a newly started help viewer and before starting another one, just check if the old one is still alive.
int id = ...
try
{
var proc = Process.GetProcessById(id);
}
catch
{
// no process running with that id
}
I am tracking multiple instances of the same application and need to get the memory and cpu use of both processes. However, I cant seem to figure out a way to use the performance counter and know which result is for which process. I have seen that I can append #1 and such to the end of the name to get results for each, but that doesn't tell me which one is for which process.
How can I determine the ProcessId or pass the process ID to the counter to get the result per each process with same name?
PerformanceCounterCPU.CategoryName = "Process";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;
PerformanceCounterMemory.CategoryName = "Process";
PerformanceCounterMemory.CounterName = "Working Set - Private";
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;
This answer to a related question might work:
private static string GetProcessInstanceName(int pid)
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
string[] instances = cat.GetInstanceNames();
foreach (string instance in instances)
{
using (PerformanceCounter cnt = new PerformanceCounter("Process",
"ID Process", instance, true))
{
int val = (int) cnt.RawValue;
if (val == pid)
{
return instance;
}
}
}
throw new Exception("Could not find performance counter " +
"instance name for current process. This is truly strange ...");
}
If you don't mind a machine-wide registry change, you can configure Windows to use the form ProcessName_ProcessID for Perf Counter instance names, rather than appending #1, #2, etc:
Create DWORD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance\ProcessNameFormat and set its value to 2.
If you do stick with the #1, #2 etc form, beware that the instance name for a given process can change during the process' lifetime!