/// <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; } }
/// <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(); } }
public static void LogSeries(string basename, ChartType type, string series, string message) { if (!IsRunning) { return; } var str = $"{HiResDateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss.fff},{series},{message}"; var chartsLogItem = new ChartLogItem { LogBaseName = basename, ChartType = type, LogCode = ChartLogCode.Series, Message = str }; AddEntry(chartsLogItem); }
public static void LogPoint(string basename, ChartType type, PointModel point) { if (!IsRunning) { return; } var str = $"{point.DateTime.ToLocalTime():yyyy-MM-dd HH:mm:ss.fff},{point.Value},{point.Set}"; var chartsLogItem = new ChartLogItem { LogBaseName = basename, ChartType = type, LogCode = ChartLogCode.Point, Message = str }; AddEntry(chartsLogItem); }
public static void LogData(string basename, ChartType type, string key, string value) { if (!IsRunning) { return; } var str = $"{HiResDateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss.fff},{key},{value}"; var chartsLogItem = new ChartLogItem { LogBaseName = basename, ChartType = type, LogCode = ChartLogCode.Data, Message = str }; AddEntry(chartsLogItem); }
/// <summary> /// adds a item to a blocking queue /// </summary> /// <param name="logitem"></param> private static void AddEntry(ChartLogItem logitem) { _chartBlockingCollection.TryAdd(logitem); }