You actually don't need to restart the system to hook to ASP.NET. All you need to do is restart IIS. Here is the code I use to hook/unhookmy profiler. You can simply call Run() and Stop(). The PROJECT_NAME and LOG_FILENAME are related to my own profiler and are not used by the .net profiling api.
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Diagnostics;
namespace ASPProfiler { delegate void ProfilerStartHandler(); delegate void ProfilerEndHandler();
class ProfilerRunner {
#region *** Fields *** private string m_profilerGuid; private string m_logFile; private string m_projectName; #endregion
#region *** Events *** public event ProfilerStartHandler Start; public event ProfilerEndHandler End; #endregion
#region *** Constructor *** public ProfilerRunner(string profilerGuid, string logFile, string projectName) { m_profilerGuid = profilerGuid; m_logFile = logFile; m_projectName = projectName; } #endregion
#region *** Methods ***
#region *** Public *** public void Run() { SetRegistry(); StartIISreset();
AnnounceStart(); }
public void Stop() { DeleteRegistry(); StartIISreset();
AnnounceEnd(); } #endregion
#region *** Private *** private void SetRegistry() { using (RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) { regKey.SetValue("COR_PROFILER", m_profilerGuid, RegistryValueKind.String); regKey.SetValue("COR_ENABLE_PROFILING", "1", RegistryValueKind.String); regKey.SetValue("LOG_FILENAME", m_logFile, RegistryValueKind.String); regKey.SetValue("PROJECT_NAME", m_projectName, RegistryValueKind.String); } }
private void DeleteRegistry() { using (RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) { regKey.DeleteValue("COR_PROFILER"); regKey.DeleteValue("COR_ENABLE_PROFILING"); regKey.DeleteValue("LOG_FILENAME"); regKey.DeleteValue("PROJECT_NAME"); } }
private void StartIISreset() { ProcessStartInfo pStartInfo;
pStartInfo = new ProcessStartInfo("iisreset.exe");
Process p = new Process(); p.StartInfo = pStartInfo; p.Start(); p.WaitForExit(); }
private void AnnounceStart() { if (Start != null) Start(); }
private void AnnounceEnd() { if (End != null) End(); } #endregion
#endregion
} }
|