private void ThreadProc() { try { while (true) { int readyCount = GetReadyCount(); if (readyCount > 0) { DoWriteLogs(readyCount); _lastTime = DateTime.Now; } else { Thread.Sleep(DefaultSleep); } } } catch (ThreadAbortException) { FlushCache(); } catch (Exception ex) { try { SystemTrace.Error(EventID.SERVICE_ERROR, GetTracingSource(), ex.ToString()); } catch { // ... // I can do nothing } } }
private void DoWriteLogs(int count) { // // Dequeue Events TracingEvent[] evts = new TracingEvent[count]; lock (_queue) { for (int i = 0; i < count; i++) { evts[i] = _queue.Dequeue(); } } if (!_textAppender.Enabled) { return; } // // Writer Text Tracing if (_textAppender.Enabled || _textAppender.BackupForDbError) { try { _textAppender.DoAppend(evts); } catch (Exception ex) { SystemTrace.Error(EventID.SERVICE_ERROR, GetTracingSource(), TracingUtils.FormatException(ex)); } } }
private TracingCacheThread() { if (TracingConfiguration.Current == null) { SystemTrace.Error(EventID.CONFIG_FAILED, "LoadingSky:Tracing Configuration Failed"); throw new Exception("Get LoadingSky:Tracing Configuration Failed, Please Check Config!"); } string path = TracingConfiguration.Current.TextAppender.Path; bool console = TracingConfiguration.Current.TextAppender.Console; _textAppender = new TextAppender(path, console); _textAppender.Enabled = TracingConfiguration.Current.TextAppender.Enable; _textAppender.BackupForDbError = false; _thread = new Thread(ThreadProc); _thread.IsBackground = true; _thread.Start(); }
public void FlushCache() { try { int count; while ((count = _queue.Count) > 0) { if (count >= BatchCount) { DoWriteLogs(BatchCount); } else { DoWriteLogs(count); } } } catch (Exception ex) { SystemTrace.Error(EventID.SERVICE_ERROR, GetTracingSource(), "Tracing Flush Cache Failed: \r\n" + TracingUtils.FormatException(ex)); } }
public void DoAppend(TracingEvent[] logEvents) { int retryCount = 0; while (retryCount < 3) { string path = retryCount == 0 ? _path + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt" : _path + DateTime.Now.ToString("yyyy-MM-dd HH [") + retryCount + "].txt"; try { using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read)) { StreamWriter sw = new StreamWriter(fs); foreach (TracingEvent evt in logEvents) { string log = GetLogString(evt); sw.Write(log); if (_console) { Console.WriteLine(log); } } sw.Close(); } } catch (IOException) { retryCount++; continue; } catch (Exception ex) { SystemTrace.Error(EventID.SERVICE_HINT, "Tracing", TracingUtils.FormatException(ex)); } return; } SystemTrace.Error(EventID.SERVICE_HINT, "Tracing", "IOException after try max count."); }