Beispiel #1
0
        /// <summary>
        /// Manages the sequential traces.
        /// </summary>
        /// <returns>ManageSequentialTraceResults.</returns>
        public ManageSequentialTraceResults ManageSequentialTraces()
        {
            lock (LogFileLock)
            {
                ManageSequentialTraceResults results = ManageSequentialTraceResults.True;

                // get all trace files .etl and prune
                List <string> etwLogs      = EnumerateLogFiles(_logFileBaseName, "[0-9]{0,4}", true);
                int           currentIndex = 0;

                // get current etw index and restart etw if max is met
                foreach (string etwLog in etwLogs)
                {
                    string currentIndexString = Regex.Match(Path.GetFileName(etwLog), @".*?\.([0-9]{0,4})\.").Groups[1].Value;
                    if (_cdfMonitor.Controller != null &&
                        !string.IsNullOrEmpty(currentIndexString) &&
                        Int32.TryParse(currentIndexString, out currentIndex) &&
                        currentIndex >= LOG_FILE_MAX_COUNT)
                    {
                        _cdfMonitor.LogOutput("ManageTraceFile: ETW log count has reached max count. restarting etw.");
                        results = ManageSequentialTraceResults.Restart;
                        break;
                    }
                }

                // get current list of cdfm files and index
                List <string> cdfmLogs = EnumerateLogFiles(_logFileBaseName, @"CDFM.[0-9]{4}", false);

                // todo fix sort for combined list. should be ok as long as cdfmlogs are added
                // before newLogs...
                List <string> allLogs = new List <string>();

                foreach (string log in cdfmLogs)
                {
                    if (!string.IsNullOrEmpty(log) & !allLogs.Contains(log))
                    {
                        allLogs.Add(log);
                    }
                }

                foreach (string log in etwLogs)
                {
                    if (!string.IsNullOrEmpty(log) & !allLogs.Contains(log))
                    {
                        allLogs.Add(log);
                    }
                }

                allLogs = PruneLogFiles(allLogs);

                Logs = allLogs.ToArray();

                if (Logs.Length < 1)
                {
                    _cdfMonitor.LogOutput("ManageTraceFile: no logs to process.");
                    return(results);
                }

                if (etwLogs.Count > 0)
                {
                    LogFileName = etwLogs.Max();
                }

                // if only one newLog (log that has not been converted to *.CDFM.*) and trace is
                // running, return
                if (etwLogs.Count == 1 &&
                    _cdfMonitor.Controller != null &&
                    _cdfMonitor.Controller.Running)
                {
                    _cdfMonitor.LogOutput("ManageTraceFile: controller running and only one file. returning.");
                    return(results);
                }

                // exit out if limit reached
                if (Logs.Length >= _job.LogFileMaxCount && !_job.LogFileOverWrite)
                {
                    _cdfMonitor.LogOutput("ManageTraceFile: max file count reached. stopping trace:" +
                                          Logs.Length.ToString());
                    _cdfMonitor.EtwTraceStop();
                    CDFMonitor.CloseCurrentSessionEvent.Set();
                    return(ManageSequentialTraceResults.False);
                }

                // get current list of cdfm files and index
                if (etwLogs.Count < 1)
                {
                    _cdfMonitor.LogOutput("ManageTraceFile: no new logs to process.");
                    return(results);
                }

                // get next file name
                for (int i = 0; i < Logs.Length; i++)
                {
                    _cdfMonitor.LogOutput("ManageTraceFile: checking file:" + Logs[i]);

                    // if cdfm file then skip
                    if (cdfmLogs.Contains(Logs[i]))
                    {
                        continue;
                    }

                    string newFile = GenerateNewLogName(cdfmLogs.Count > 0 ? cdfmLogs.Last() :
                                                        Regex.Replace(_logFileBaseName, Path.GetExtension(_logFileBaseName), ".CDFM" + Path.GetExtension(_logFileBaseName)));
                    _cdfMonitor.LogOutput(string.Format("ManageSequentialTraces: renaming file {0} to {1}",
                                                        Logs[i], newFile));
                    if (!FileManager.FileInUse(Logs[i]))
                    {
                        FileManager.RenameFile(Logs[i], newFile);
                        Logs[i] = newFile;
                        cdfmLogs.Add(newFile);
                    }
                    else
                    {
                        _cdfMonitor.LogOutput(
                            string.Format("DEBUG:ManageSequentialTraces: file in use cannot rename file {0} to {1}",
                                          Logs[i], newFile));
                    }
                }

                Logs = cdfmLogs.ToArray();
                if (LogFileServerEvt != null)
                {
                    LogFileServerEvt.Set();
                }

                return(results);
            }
        }
Beispiel #2
0
        /// <summary>
        /// determines if multiple files are needed trims files if out of range which file is
        /// current
        /// </summary>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public bool ManageSequentialLogs()
        {
            lock (LogFileLock)
            {
                // Reformat logFileName to include version number
                // Search directory for existing files in format of Config.LogFileName.####.ext

                if (!LoggingIsEnabled)
                {
                    return(true);
                }

                string        logFile  = LogFileName;
                List <string> tempList = PruneLogFiles(EnumerateLogFiles(logFile, "[0-9]{0,4}", true));

                // set current log
                if (tempList.Count > 0)
                {
                    logFile = tempList[tempList.Count - 1];
                }
                else
                {
                    // no log files so use default
                    logFile = GenerateNewLogName(logFile);
                    tempList.Add(logFile);
                }

                FileInfo logFileInfo = new FileInfo(logFile);

                // exit out if out of size or count
                if (!_job.LogFileOverWrite &&
                    logFileInfo.Exists &&
                    _job.LogFileMaxCount > 0 &&
                    tempList.Count >= _job.LogFileMaxCount &&
                    _job.LogFileMaxSize > 0 &&
                    logFileInfo.Length >= _job.LogFileMaxSizeBytes)
                {
                    _cdfMonitor.EtwTraceStop();
                    _cdfMonitor.LogOutput("DEBUG:ManageSequentialLogs:reached max logfile size limit/count. quitting");
                    CDFMonitor.CloseCurrentSessionEvent.Set();
                    return(false);
                }

                DisableLogStream();

                // We have complete logs list so now if logfilename doesnt exist, set and exit
                // File exists and has no more room
                if (FileManager.FileInUse(logFile) ||
                    (_job.LogFileMaxSize > 0 &&
                     logFileInfo.Exists &&
                     logFileInfo.Length >= _job.LogFileMaxSizeBytes))
                {
                    if (_job.LogFileMaxCount > 0 &&
                        tempList.Count >= _job.LogFileMaxCount)
                    {
                        // Delete oldest and generate new name
                        FileManager.DeleteFile(tempList[0]);
                        tempList.Remove(tempList[0]);
                    }

                    logFile = GenerateNewLogName(logFile);
                    tempList.Add(logFile);
                }

                CurrentLogFileSize = File.Exists(logFile) ? new FileInfo(logFile).Length : 0;

                Logs = tempList.ToArray();
                bool retval = SetLogFileName(logFile);
                LoggingIsEnabled = true;
                _job.Enabled     = true;
                if (LogFileServerEvt != null)
                {
                    LogFileServerEvt.Set();
                }

                return(retval);
            }
        }