Example #1
0
        static ChartLogging()
        {
            try
            {
                _instanceFileName = $"{DateTime.Now:yyyy-dd-MM}.txt";
                DeleteFiles(_fileNameAddOn, 7, _logPath);

                _chartBlockingCollection = new BlockingCollection <ChartLogItem>();
                Task.Factory.StartNew(() =>
                {
                    foreach (var logitem in _chartBlockingCollection.GetConsumingEnumerable())
                    {
                        ProcessChartQueueItem(logitem);
                    }
                });

                IsRunning = true;
            }
            catch (Exception ex)
            {
                IsRunning = false;
                var monitorItem = new MonitorEntry
                {
                    Datetime = HiResDateTime.UtcNow,
                    Device   = MonitorDevice.Server,
                    Category = MonitorCategory.Server,
                    Type     = MonitorType.Error,
                    Method   = MethodBase.GetCurrentMethod().Name,
                    Thread   = Thread.CurrentThread.ManagedThreadId,
                    Message  = $" {ex.Message}"
                };
                MonitorLog.LogToMonitor(monitorItem);
            }
        }
Example #2
0
 /// <summary>
 /// Process item from the blocking queue
 /// </summary>
 /// <param name="logitem"></param>
 private static void ProcessChartQueueItem(ChartLogItem logitem)
 {
     try
     {
         if (logitem.Message != string.Empty)
         {
             FileWriteAsync(_fileLocation + "GS" + logitem.LogBaseName + _fileNameAddOn + _instanceFileName, logitem);
         }
     }
     catch (Exception ex)
     {
         var monitorItem = new MonitorEntry
         {
             Datetime = HiResDateTime.UtcNow,
             Device   = MonitorDevice.Server,
             Category = MonitorCategory.Server,
             Type     = MonitorType.Error,
             Method   = MethodBase.GetCurrentMethod().Name,
             Thread   = Thread.CurrentThread.ManagedThreadId,
             Message  = $" {ex.Message}"
         };
         MonitorLog.LogToMonitor(monitorItem);
         IsRunning = false;
     }
 }
Example #3
0
        /// <summary>
        /// Send entries to a file async
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="logitem"></param>
        /// <param name="append"></param>
        private static async void FileWriteAsync(string filePath, ChartLogItem logitem, bool append = true)
        {
            await _lockFile.WaitAsync();

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath) ?? throw new InvalidOperationException());
                using (var stream = new FileStream(filePath, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
                    using (var sw = new StreamWriter(stream))
                    {
                        var str = $"{(int)logitem.ChartType},{(int)logitem.LogCode},{logitem.Message}";
                        await sw.WriteLineAsync(str);
                    }
            }
            catch (IOException ex)
            {
                var monitorItem = new MonitorEntry
                {
                    Datetime = HiResDateTime.UtcNow,
                    Device   = MonitorDevice.Server,
                    Category = MonitorCategory.Server,
                    Type     = MonitorType.Error,
                    Method   = MethodBase.GetCurrentMethod().Name,
                    Thread   = Thread.CurrentThread.ManagedThreadId,
                    Message  = $" {ex.Message}"
                };
                MonitorLog.LogToMonitor(monitorItem);
            }
            finally
            {
                _lockFile.Release();
            }
        }
Example #4
0
 /// <summary>
 /// Log multiple entries to the monitor
 /// </summary>
 public static void LogToMonitor(MonitorEntry entry, IEnumerable <MonitorCategory> categories)
 {
     foreach (var category in categories)
     {
         entry.Category = category;
         LogToMonitor(entry);
     }
 }
Example #5
0
        /// <summary>
        /// Send a MonitorEntry to the queue to be processed
        /// </summary>
        public static void LogToMonitor(MonitorEntry entry)
        {
            entry.Message = entry.Message.Trim();
            entry.Method  = entry.Method.Trim();
            // don't add to queue if not needed
            switch (entry.Type)
            {
            case MonitorType.Warning:
            case MonitorType.Error:
            case MonitorType.Information:
                MonitorQueue.AddEntry(entry);
                break;

            case MonitorType.Data:
                if (GetJEntries || Settings.StartMonitor)
                {
                    MonitorQueue.AddEntry(entry);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }