private static void ThreadProcWriteLog() { try { do { TraceLogInfo logInfo = DequeueLogInfo(); if (IsLogFileSizeLimitCrossed(logInfo)) { logInfo = null; } if (logInfo != null) { if (logInfo.TraceID.Trim().Length == 0) { logInfo.TraceID = defTraceID; } string traceFileName = GetTraceFileName(logInfo.TraceID); if (traceFileName != string.Empty) { StreamWriter writer = null; try { if (!File.Exists(traceFileName)) { WriteAppDetail(logInfo); } writer = new StreamWriter(traceFileName, true, m_encoding); string str2 = string.Format("{0:yyyy/MM/dd HH:mm:ss} {1} {2:000} {3}", new object[] { DateTime.Now, (int)logInfo.TraceLevel, Convert.ToInt32(logInfo.ThreadID), logInfo.Message }); if (ShowInConsole) { Console.WriteLine("[{0}] :: {1}", logInfo.TraceID, str2); } writer.WriteLine(str2 + " Memory Used:" + ((double)((Process.GetCurrentProcess().WorkingSet64 / 0x400L) / 0x400L)).ToString("#0.00") + "MB"); } catch (Exception exception) { Console.WriteLine(exception.ToString()); } finally { if (writer != null) { writer.Flush(); writer.Close(); } } } } }while (_logInfoCnt > 0); } catch { } finally { m_bWrittingToLogFile = false; } }
public static void WriteLog(TraceLogInfo logInfo) { if ((logInfo.TraceLevel != eTRACELEVEL.DISABLED) && (logInfo.TraceLevel <= TraceLogLevel)) { logInfo.ThreadID = Thread.CurrentThread.ManagedThreadId.ToString(); EnqueueLogInfo(logInfo); } }
private static void EnqueueLogInfo(TraceLogInfo logInfo) { lock (_logQueue.SyncRoot) { if (IsLogFileSizeLimitCrossed(logInfo)) { deleteTraceLogFile(logInfo); } _logQueue.Enqueue(logInfo); Interlocked.Increment(ref _logInfoCnt); } }
private static TraceLogInfo DequeueLogInfo() { TraceLogInfo info = null; if (_logInfoCnt > 0) { lock (_logQueue.SyncRoot) { info = (TraceLogInfo)_logQueue.Dequeue(); Interlocked.Decrement(ref _logInfoCnt); } } return(info); }
private static bool IsLogFileSizeLimitCrossed(TraceLogInfo logInfo) { if (_logSizeLimit <= 0) { return(false); } string traceFileName = GetTraceFileName(logInfo.TraceID); if ((traceFileName == null) || (traceFileName.Trim().Length == 0)) { return(false); } FileInfo info = new FileInfo(traceFileName); return(((info != null) && info.Exists) && (((info.Length / 0x400L) / 0x400L) >= _logSizeLimit)); }
private static void WriteAppDetail(TraceLogInfo logInfo) { string traceFileName = GetTraceFileName(logInfo.TraceID); if (traceFileName != string.Empty) { StreamWriter writer = null; try { if (!File.Exists(traceFileName) && (writer == null)) { writer = new StreamWriter(traceFileName, true, m_encoding); } if (writer == null) { writer = new StreamWriter(traceFileName, true, m_encoding); } FileInfo info = new FileInfo(Application.ExecutablePath); string str2 = string.Format("{0:yyyy/MM/dd HH:mm:ss} {1} {2:000} Application Name [{3}] Version No [{4}] Last Build Date Time [{5:yyyy/MM/dd HH:mm:ss}]", new object[] { DateTime.Now, (int)logInfo.TraceLevel, Convert.ToInt32(logInfo.ThreadID), info.Name, Application.ProductVersion, info.CreationTime }); if (ShowInConsole) { Console.WriteLine("[{0}] :: {1}", logInfo.TraceID, str2); } writer.WriteLine(); writer.WriteLine(str2); writer.WriteLine(); writer.WriteLine(); } catch (Exception exception) { Console.WriteLine(exception.ToString()); } finally { if (writer != null) { writer.Flush(); writer.Close(); } } } }
private static bool deleteTraceLogFile(TraceLogInfo logInfo) { string traceFileName = GetTraceFileName(logInfo.TraceID); if ((traceFileName == null) || (traceFileName.Trim().Length == 0)) { return(false); } FileInfo info = new FileInfo(traceFileName); if ((info != null) && info.Exists) { try { info.Delete(); } catch (Exception) { return(false); } } return(true); }