/// <summary> /// Enqueues the TaceLogInfo in the queue to be written in the log file /// Writing is done by ThreadProcWriteLog() proc /// </summary> /// <param name="logInfo">Trace Log Information</param> public static void WriteLog(TraceLogInfo logInfo) { if ((logInfo.TraceLevel == eTRACELEVEL.DISABLED)) { return; // TODO: might not be correct. Was : Exit Sub } if ((logInfo.TraceLevel > TraceLogLevel)) { return; // TODO: might not be correct. Was : Exit Sub } logInfo.ThreadID = Thread.CurrentThread.ManagedThreadId.ToString(); //Add the Log trace info to the queue. EnqueueLogInfo(logInfo); //2) Check if a process is already running that is writing to log file or not. // if already running then do noting. // else start thread that writes into the traceFiles. //If Not m_bWrittingToLogFile Then // m_bWrittingToLogFile = True // Dim thrdStrt As ThreadStart = New ThreadStart(AddressOf ThreadProcWriteLog) // Dim thrd As Thread = New Thread(thrdStrt) // thrd.IsBackground = True // thrd.Name = "ThreadWriteLogTraceToFile" // thrd.Start() //End If }
/// <summary> /// deletes log file /// </summary> /// <param name="logInfo">logInfo</param> /// <returns>true=if success,else guess?</returns> private static bool deleteTraceLogFile(TraceLogInfo logInfo) { string fileName = GetTraceFileName(logInfo.TraceID); if (fileName == null || fileName.Trim().Length == 0) { return(false); } FileInfo info = new FileInfo(fileName); if (info != null) { if (info.Exists) { try { info.Delete(); } catch (Exception) { return(false); } } } return(true); }
private static bool IsLogFileSizeLimitCrossed(TraceLogInfo logInfo) { //if the limit is set to 0 then no need to perform the checking. if (_logSizeLimit <= 0) { return(false); } if (logInfo == null) { return(false); } string fileName = GetTraceFileName(logInfo.TraceID); if (fileName == null || fileName.Trim().Length == 0) { return(false); } FileInfo info = new FileInfo(fileName); if (info != null) { if (info.Exists) { if ((info.Length / 1024 / 1024) >= _logSizeLimit) { return(true); } } } return(false); }
/// <summary> /// Writes Application detail in log file. /// If the file is creating for the first time then put some file information on the top of the file. /// Each file should contain exe information at the top. /// Message format is Application Name = [xxxxx] Version No = [0.0.0.0] Last Build Date Time = [yyyy/MM/dd hh:mm:ss] /// </summary> /// <param name="logInfo">Log Trace Information</param> private static void WriteAppDetail(TraceLogInfo logInfo) { return; //string fileName = GetTraceFileName(logInfo.TraceID); //if ((fileName != string.Empty)) //{ // StreamWriter wr = null; // try // { // //if the file does not exists and being created then update the program version information into the file. // if (!File.Exists(fileName)) // { // if (wr == null) // wr = new StreamWriter(fileName, true, m_encoding); // } // if (wr == null) // wr = new StreamWriter(fileName, true, m_encoding); // //TODO // //FileInfo inf = null;//new FileInfo(System.Windows.Forms.Application.ExecutablePath); // //3) Writes into the logfile // //Format for writing the trace log // // yyyy/MM/dd HH:mm:ss [intLevel] [strLog] // //Exe information should be in the format // //Application Name = [xxxxx] Version No = [0.0.0.0] Last Build Date Time = [yyyy/MM/dd hh:mm:ss] // /*string strMsg = 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}]", DateTime.Now, (int)logInfo.TraceLevel, Convert.ToInt32(logInfo.ThreadID), inf.Name, System.Windows.Forms.Application.ProductVersion, inf.CreationTime);*/ // string strMsg = "test"; // //display the log message into console. // if ((ShowInConsole)) // { // System.Console.WriteLine("[{0}] :: {1}", logInfo.TraceID, strMsg); // } // wr.WriteLine(); // wr.WriteLine(strMsg); // //Write 2 blank lines // wr.WriteLine(); // wr.WriteLine(); // } // catch (Exception ex) // { // Console.WriteLine(ex.ToString()); // } // //Do nothing // finally // { // if ((wr != null)) // { // wr.Flush(); // wr.Close(); // } // } //} }
/// <summary> /// Dequeues LogInformation from the queue /// </summary> /// <returns>TraceLogInfo object.</returns> private static TraceLogInfo DequeueLogInfo() { TraceLogInfo logInfo = null; if ((_logInfoCnt > 0)) { //Lock the queue lock ((_logQueue.SyncRoot)) { //Add Log Info into the queue. logInfo = (TraceLogInfo)_logQueue.Dequeue(); //decrement the log information counter. System.Threading.Interlocked.Decrement(ref _logInfoCnt); } } return(logInfo); }
/// <summary> /// Adds LogInformation into the queue. /// </summary> /// <param name="logInfo">Trace Log Information</param> private static void EnqueueLogInfo(TraceLogInfo logInfo) { //Lock the queue lock ((_logQueue.SyncRoot)) { if (IsLogFileSizeLimitCrossed(logInfo)) { //delete log file deleteTraceLogFile(logInfo); } //Add Log Info into the queue. _logQueue.Enqueue(logInfo); //increment the log information counter. System.Threading.Interlocked.Increment(ref _logInfoCnt); } }