Пример #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 void DequeueAnalysisRequest()
        {
            string analysisFolderPath = GetAnalysisFolderPath(out bool errorEncountered);

            if (!errorEncountered)
            {
                try
                {
                    string requestFile = FileSystemHelpers.GetFilesInDirectory(analysisFolderPath, "*.request", false, SearchOption.TopDirectoryOnly).FirstOrDefault();

                    if (requestFile != null)
                    {
                        var analysisRequest = FileSystemHelpers.FromJsonFile <AnalysisRequest>(requestFile);
                        var isRequestFileFromSameInstance = analysisRequest.LogFileName.StartsWith(Environment.MachineName);
                        var exceededAnalysisTimelimit     = analysisRequest.StartTime != null && DateTime.UtcNow.Subtract(analysisRequest.StartTime).TotalMinutes > MAX_ANALYSISTIME_ON_SAME_INSTANCE;

                        Logger.LogCpuMonitoringVerboseEvent($"Checking AnalysisRequest LogFileName={analysisRequest.LogFileName}, MachineName={Environment.MachineName}, exceededAnalysisTimelimit={exceededAnalysisTimelimit}, isRequestFileFromSameInstance={isRequestFileFromSameInstance}", analysisRequest.SessionId);

                        if (exceededAnalysisTimelimit || isRequestFileFromSameInstance)
                        {
                            var inprogressFile = Path.ChangeExtension(requestFile, ".inprogress");
                            FileSystemHelpers.MoveFile(requestFile, inprogressFile);
                            AnalyzeKeepingExpirationAlive(inprogressFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogCpuMonitoringErrorEvent("Failure in DequeueAnalysisRequest", ex, string.Empty);
                }
            }
        }
Пример #3
0
        private void MoveMonitoringLogsToSession(string sessionId)
        {
            string logsFolderPath         = CpuMonitoring.GetLogsFolderForSession(sessionId);
            string monitoringFolderActive = GetCpuMonitoringPath(MonitoringSessionDirectories.Active);
            var    filesCollected         = FileSystemHelpers.GetFilesInDirectory(monitoringFolderActive, "*.log", false, SearchOption.TopDirectoryOnly);

            foreach (string monitoringLog in filesCollected)
            {
                string fileName = Path.GetFileName(monitoringLog);
                fileName = Path.Combine(logsFolderPath, fileName);
                Logger.LogCpuMonitoringVerboseEvent($"Moving {monitoringLog} to {fileName}", sessionId);
                RetryHelper.RetryOnException("Moving monitoring log to logs folder...", () =>
                {
                    FileSystemHelpers.MoveFile(monitoringLog, fileName);
                }, TimeSpan.FromSeconds(5), 5);
            }
        }