I have a pretty straight forward event handler on process exit that wants to log the process name and exit code. This code runs perfectly in my local machine (Windows 10 pro, 64 bit) no matter how I end the process (calling Kill(), End Task from Task Manager or closing the app). However the deployed version of the same code in Integration env (Windows Server 2016 DC, 64 bit) is failing with below error.
System.InvalidOperationException: Process has exited, so the requested information is not available. at System.Diagnostics.Process.EnsureState(State state) at System.Diagnostics.Process.get_ProcessName()
Any resolutions/pointers in troubleshooting this will be helpful.
Event handler looks like below:
private static void MyProcess_Exited(object sender, EventArgs e)
{
var process = sender as Process;
var processExitLog = new StringBuilder()
.AppendLine($"{process.ProcessName} exited with code {process.ExitCode} at {process.ExitTime.ToString("s")}.")
.AppendLine($"Total uptime: {(process.ExitTime - process.StartTime).ToString("c")}")
.AppendLine($"Running threads: {process.Threads.Count}")
.AppendLine($"Peak physical memory utilization was: {process.PeakWorkingSet64}.")
.ToString();
Console.WriteLine(processExitLog);
Console.WriteLine();
}