示例#1
0
        private void AppendToMonitoringLog(string message, bool logInKusto = false)
        {
            Interlocked.Increment(ref _loggerCount);
            string cpuMonitorPath = MonitoringSessionController.GetCpuMonitoringPath(MonitoringSessionDirectories.Active);
            string logFilePath    = Path.Combine(cpuMonitorPath, Environment.MachineName + ".log");

            string logMessage = $"[{DateTime.UtcNow.ToShortDateString()} {DateTime.UtcNow.ToString("hh:mm:ss")}] {message}{Environment.NewLine}";

            if (_loggerCount > MAX_LINES_IN_LOGFILE)
            {
                var sessionDirectory  = GetLogsFolderForSession(_sessionId);
                var existingFileCount = FileSystemHelpers.GetFilesInDirectory(sessionDirectory, $"{Environment.MachineName}*.log", false, SearchOption.TopDirectoryOnly).Count;
                var newFileName       = $"{Environment.MachineName}_{existingFileCount}.log";
                newFileName = Path.Combine(sessionDirectory, newFileName);
                FileSystemHelpers.MoveFile(logFilePath, newFileName);
                Interlocked.Exchange(ref _loggerCount, 0);
            }

            try
            {
                FileSystemHelpers.AppendAllTextToFile(logFilePath, logMessage);
            }
            catch (Exception)
            {
            }

            if (logInKusto)
            {
                Logger.LogCpuMonitoringEvent(message, _sessionId);
            }
        }
示例#2
0
        public static string GetLogsFolderForSession(string sessionId)
        {
            string logsFolderPath = MonitoringSessionController.GetCpuMonitoringPath(MonitoringSessionDirectories.Logs);
            string folderName     = Path.Combine(logsFolderPath, sessionId);

            FileSystemHelpers.CreateDirectoryIfNotExists(folderName);
            return(folderName);
        }
示例#3
0
        public static string GetRelativePath(string sessionId, string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return(string.Empty);
            }
            var    logsFolderPath = MonitoringSessionController.GetCpuMonitoringPath(MonitoringSessionDirectories.Logs, true);
            string path           = Path.Combine(logsFolderPath, sessionId, fileName);

            return(path.ConvertBackSlashesToForwardSlashes());
        }
示例#4
0
        public static void CleanRemainingLogsIfAny()
        {
            string cpuMonitorPath = MonitoringSessionController.GetCpuMonitoringPath(MonitoringSessionDirectories.Active);

            if (FileSystemHelpers.DirectoryExists(cpuMonitorPath))
            {
                foreach (var log in FileSystemHelpers.GetFilesInDirectory(cpuMonitorPath, "*.log", false, SearchOption.TopDirectoryOnly))
                {
                    FileSystemHelpers.DeleteFileSafe(log);
                }
            }
        }
示例#5
0
 static void UpdateSession(string sessionId, string logfileName, string reportFilePath, List <string> errors = null, bool shouldUpdateSessionStatus = true)
 {
     try
     {
         var sessionController = new MonitoringSessionController();
         sessionController.AddReportToLog(sessionId, logfileName, reportFilePath, errors, shouldUpdateSessionStatus);
     }
     catch (Exception ex)
     {
         Logger.LogCpuMonitoringErrorEvent("Failed while updating session", ex, sessionId);
     }
 }
示例#6
0
        public static string GetAnalysisFolderPath(out bool errorEncountered)
        {
            errorEncountered = false;
            string path = Path.Combine(Settings.UserSiteStorageDirectory, MonitoringSessionController.GetCpuMonitoringPath(), MonitoringSessionDirectories.Analysis);

            try
            {
                FileSystemHelpers.CreateDirectoryIfNotExists(path);
            }
            catch (Exception ex)
            {
                errorEncountered = true;
                Logger.LogCpuMonitoringErrorEvent("Failure in GetAnalysisFolderPath", ex, string.Empty);
            }
            return(path);
        }
示例#7
0
        private static string AnalyzeDumpFile(AnalysisRequest analysisRequest, string inprogressFile, CancellationToken token)
        {
            var blobSasUri = analysisRequest.BlobSasUri;

            string cpuMonitorPath      = MonitoringSessionController.GetCpuMonitoringPath(MonitoringSessionDirectories.Logs);
            var    diagnosticToolsPath = Infrastructure.Settings.GetDiagnosticToolsPath();
            string outputPath          = Path.Combine(cpuMonitorPath, analysisRequest.SessionId);
            string inputFile           = CacheFileInTempDirectory(analysisRequest);
            string args    = $@"-File ""{diagnosticToolsPath}\DumpAnalyzer.ps1"" ""{inputFile}"" ""{outputPath}""";
            var    command = @"D:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";

            Logger.LogCpuMonitoringVerboseEvent($"Powershell started with args [{args}]", analysisRequest.SessionId);
            double secondsWaited    = 0;
            var    processStartTime = DateTime.UtcNow;
            var    toolProcess      = Infrastructure.RunProcess(command, args, analysisRequest.SessionId);

            while (!toolProcess.HasExited)
            {
                //
                // Keep updating the Expiration time while we are waiting for the DumpAnalyzer to finish
                //
                analysisRequest.ExpirationTime = DateTime.UtcNow.AddMinutes(ANALYSIS_HEARTBEAT_EXPIRATION_IN_MINUTES);
                analysisRequest.ToJsonFile(inprogressFile);

                Thread.Sleep(10 * 1000);
                secondsWaited = secondsWaited + 10;

                if (secondsWaited > 120)
                {
                    secondsWaited = 0;
                    Logger.LogCpuMonitoringVerboseEvent($"Waiting for Analysis process {command} {args} to finish. Process running for {DateTime.UtcNow.Subtract(processStartTime).TotalSeconds} seconds", analysisRequest.SessionId);
                }

                if (token != CancellationToken.None && token.IsCancellationRequested)
                {
                    Logger.LogCpuMonitoringVerboseEvent($"Kill tool process [{command} {args}] because cancellation is requested", analysisRequest.SessionId);
                    toolProcess.SafeKillProcess();

                    foreach (var dumpAnalyzer in Process.GetProcesses().Where(x => x.ProcessName.Equals("DumpAnalyzer", StringComparison.OrdinalIgnoreCase)))
                    {
                        Logger.LogCpuMonitoringVerboseEvent($"Going to kill [DumpAnalyzer ({dumpAnalyzer.Id})] because cancellation is requested", analysisRequest.SessionId);
                        dumpAnalyzer.SafeKillProcess();
                    }

                    token.ThrowIfCancellationRequested();
                }
            }

            // Delete the file in the temp directory once analysis is done
            FileSystemHelpers.DeleteFileSafe(inputFile);

            if (toolProcess.ExitCode != 0)
            {
                throw new Exception($"Analysis process exited with error code {toolProcess.ExitCode}");
            }

            var reportNamePattern = Path.GetFileNameWithoutExtension(analysisRequest.LogFileName) + "*.mht";
            var reportFilePath    = FileSystemHelpers.GetFilesInDirectory(outputPath, reportNamePattern).FirstOrDefault();

            Logger.LogCpuMonitoringVerboseEvent($"DumpAnalyzer completed and reportPath is [{reportFilePath}]", analysisRequest.SessionId);
            return(reportFilePath);
        }