应用原因:我做了一个C/S系统。很多客户端连接到服务器来进行数据上报的工作。但由于数据量的巨大,程序经常出现System.OutOfMemoryException的错误。
接下来我要对IIS进行应用程序池的设置,同时也需要修改程序。这时我就想知道,在出现错误时,机器的各个进程的状态。比如当前的服务器有启用哪些进程,这些进程的内存占用为多少,W3WP占用的物理内存与虚拟内存为多少。
于是我写了如下的应用程序代码:
....
ArrayList al = GetCurrentProcess(iErrorID);
foreach (NoteErrProcessInfo pInfo in al)
{
WriteProcessInfoToDataBase(pInfo);
}
/// <summary>
/// 取得当前所有线程的信息数组
/// </summary>
/// <returns>线程信息数组 </returns>
private static ArrayList GetCurrentProcess(int iErrorID)
{
ArrayList al = new ArrayList();
Process[] MyProcesses = Process.GetProcesses();
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName.ToUpper() != "IDLE")
{
al.Add(GetProcessInfo(iErrorID, MyProcess.Id));
}
}
return al;
}
/// <summary>
/// 给线程信息类赋值
/// </summary>
/// <param name="iErrorID">错误行号 </param>
/// <param name="iProcessID">线程ID值 </param>
/// <returns> </returns>
private static NoteErrProcessInfo GetProcessInfo(int iErrorID,int iProcessID)
{
NoteErrProcessInfo pInfo = new NoteErrProcessInfo();
Process MyProcess = Process.GetProcessById(iProcessID);
pInfo.ErrorID = iErrorID;
pInfo.ProcessName = MyProcess.ProcessName;
pInfo.ProcessId = MyProcess.Id.ToString();
pInfo.ThreadsCount = MyProcess.Threads.Count.ToString();
pInfo.CpuProcessorTime = MyProcess.TotalProcessorTime.ToString();
pInfo.PriorityClass = MyProcess.PriorityClass.ToString();
pInfo.StartTime = MyProcess.StartTime.ToLongTimeString();
pInfo.PrivateMemorySize = (MyProcess.PrivateMemorySize64 / 1024).ToString() + "K";
pInfo.PeakVirtualMemorySize = (MyProcess.PeakVirtualMemorySize64 / 1024).ToString() + "K";
pInfo.PeakPagedMemorySize = (MyProcess.PeakPagedMemorySize64 / 1024).ToString() + "K";
pInfo.PagedSystemMemorySize = (MyProcess.PagedSystemMemorySize64 / 1024).ToString() + "K";
pInfo.PagedMemorySize = (MyProcess.PagedMemorySize64 / 1024).ToString() + "K";
pInfo.NonpagedSystemMemorySize = (MyProcess.NonpagedSystemMemorySize64 / 1024).ToString() + "K";
pInfo.WorkingMomory = (MyProcess.WorkingSet64 / 1024).ToString() + "K";
pInfo.VirtualMemorySize = (MyProcess.VirtualMemorySize64 / 1024).ToString() + "K";
return pInfo;
}
.......
当出现应用程序错误的时候,把线程错误日志,以及线程性能日志,全部插入到数据库中去。
问题描述:
如果这份程序是winform程序,或者用F5进行调试。则没有任何问题。
但如果放在ASP.NET+IIS站点中执行的时候,会报如下错误:
MyProcess.TotalProcessorTime = “MyProcess.TotalProcessorTime”引发了“System.ComponentModel.Win32Exception”类型的异常。其中NativeErrorCode为5
根据帮助,我查询了Win32 Error Codes
具体参见:ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/debug/base/system_error_codes.htm
发现错误描述为ERROR_ACCESS_DENIED 5 Access is denied.
问题:
我想知道,在ASP.NET + IIS 组成的站点中,想实现我这样的性能监视功能,我应该怎么进行IIS的设置?或者说,怎么该我的程序?