コード例 #1
0
 public void Error(string format, params object[] args)
 {
     if (args.Length == 0)
     {
         _log.Error(format);
     }
     else
     {
         _log.Error(format, args);
     }
 }
コード例 #2
0
        private void GetPerfCounterInformation(Dictionary <string, object> stats, int count)
        {
            if (_giveup)
            {
                return;
            }
            var process = Process.GetCurrentProcess();

            try {
                stats["proc-startTime"]            = process.StartTime.ToUniversalTime().ToString("O");
                stats["proc-id"]                   = process.Id;
                stats["proc-mem"]                  = new StatMetadata(process.WorkingSet64, "Process", "Process Virtual Memory");
                stats["proc-cpu"]                  = new StatMetadata(_eventCountersHelper.GetProcCpuUsage(), "Process", "Process Cpu Usage");
                stats["proc-threadsCount"]         = _eventCountersHelper.GetProcThreadsCount();
                stats["proc-contentionsRate"]      = _eventCountersHelper.GetContentionsRateCount();
                stats["proc-thrownExceptionsRate"] = _eventCountersHelper.GetThrownExceptionsRate();

                switch (OS.OsFlavor)
                {
                case OsFlavor.Windows:
                    stats["sys-cpu"] = _perfCounter.GetTotalCpuUsage();
                    break;

                case OsFlavor.Linux:
                case OsFlavor.MacOS:
                    var loadAverages = _hostStat.GetLoadAverages();
                    stats["sys-loadavg-1m"]  = loadAverages.Average1m;
                    stats["sys-loadavg-5m"]  = loadAverages.Average5m;
                    stats["sys-loadavg-15m"] = loadAverages.Average15m;
                    break;

                default:
                    stats["sys-cpu"] = -1;
                    break;
                }

                stats["sys-freeMem"] = GetFreeMem();

                var gcStats = _eventCountersHelper.GetGcStats();
                stats["proc-gc-allocationSpeed"]   = gcStats.AllocationSpeed;
                stats["proc-gc-gen0ItemsCount"]    = gcStats.Gen0ItemsCount;
                stats["proc-gc-gen0Size"]          = gcStats.Gen0Size;
                stats["proc-gc-gen1ItemsCount"]    = gcStats.Gen1ItemsCount;
                stats["proc-gc-gen1Size"]          = gcStats.Gen1Size;
                stats["proc-gc-gen2ItemsCount"]    = gcStats.Gen2ItemsCount;
                stats["proc-gc-gen2Size"]          = gcStats.Gen2Size;
                stats["proc-gc-largeHeapSize"]     = gcStats.LargeHeapSize;
                stats["proc-gc-timeInGc"]          = gcStats.TimeInGc;
                stats["proc-gc-totalBytesInHeaps"] = gcStats.TotalBytesInHeaps;
            } catch (InvalidOperationException) {
                _log.Information("Received error reading counters. Attempting to rebuild.");
                _perfCounter = new PerfCounterHelper(_log);
                _giveup      = count > 10;
                if (_giveup)
                {
                    _log.Error("Maximum rebuild attempts reached. Giving up on rebuilds.");
                }
                else
                {
                    GetPerfCounterInformation(stats, count + 1);
                }
            }
        }
コード例 #3
0
        public bool TryProcess(CommandProcessorContext context, string[] args, out int exitCode)
        {
            var commandName = args[0].ToUpper();
            var commandArgs = args.Skip(1).ToArray();

            ICmdProcessor commandProcessor;

            if (!_processors.TryGetValue(commandName, out commandProcessor))
            {
                _log.Information("Unknown command: {command}.", commandName);
                if (_regCommandsProcessor != null)
                {
                    _regCommandsProcessor.Execute(context, new string[0]);
                }
                exitCode = 1;
                return(false);
            }

            int exitC         = 0;
            var executedEvent = new AutoResetEvent(false);

            ThreadPool.QueueUserWorkItem(_ => {
                try {
                    var syntaxOk = commandProcessor.Execute(context, commandArgs);
                    if (syntaxOk)
                    {
                        exitC = context.ExitCode;
                    }
                    else
                    {
                        exitC = 1;
                        _log.Information("Usage of {command}:{newLine}{usage}", commandName, Environment.NewLine,
                                         commandProcessor.Usage);
                    }

                    executedEvent.Set();
                } catch (Exception exc) {
                    _log.Error(exc, "Error while executing command {command}.", commandName);
                    exitC = -1;
                    executedEvent.Set();
                }
            });

            executedEvent.WaitOne(1000);
            context.WaitForCompletion();

            if (!string.IsNullOrWhiteSpace(context.Reason))
            {
                _log.Error("Error during execution of command: {e}.", context.Reason);
            }
            if (context.Error != null)
            {
                _log.Error(context.Error, "Error during execution of command");

                var details = new StringBuilder();
                BuildFullException(context.Error, details);
                _log.Error("Details: {details}", details.ToString());
            }

            exitCode = exitC == 0 ? context.ExitCode : exitC;
            return(true);
        }