/// <summary> /// Write an error message to the desired destination /// </summary> /// <param name="er">Exception to be written</param> /// <param name="erLevel">The error level of the message</param> public static void Write(Exception er, LoggerErrorLevels erLevel) { String msg = er.Message; try { if ((m_writeExtendedMessage) && (er.InnerException != null) && (er.InnerException.Message.Length > 0)) { msg += "\n" + er.InnerException.Message; } } catch (Exception ex) { Console.WriteLine(ex.Message); } Write(msg, erLevel); }
/// <summary> /// Write an error message to the desired destination /// </summary> /// <param name="msg">The message to be written</param> /// <param name="erLevel">The error level of the message</param> public static void Write(string msg, LoggerErrorLevels erLevel) { if ((!m_showDebugInfo) && (erLevel == LoggerErrorLevels.DEBUG)) { return; } string outMsg = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); switch (erLevel) { case LoggerErrorLevels.INFO: { outMsg += " INFO: "; break; } case LoggerErrorLevels.WARNING: { outMsg += " WARNING: "; break; } case LoggerErrorLevels.ERROR: { outMsg += " ERROR: "; break; } case LoggerErrorLevels.FATAL: { outMsg += " FATAL: "; break; } case LoggerErrorLevels.DEBUG: { outMsg += " DEBUG: "; break; } } string fullInfo = ""; if (erLevel != LoggerErrorLevels.INFO) { StackTrace st = new StackTrace(true); int frameIndex = 0; // skip frames not from fqnOfCallingClass while (frameIndex < st.FrameCount) { StackFrame frame = st.GetFrame(frameIndex); if (frame != null && !frame.GetMethod().DeclaringType.Name.Equals("Logger")) { break; } ++frameIndex; } //++frameIndex; while (frameIndex < st.FrameCount) { // now frameIndex is the first 'user' caller frame StackFrame locationFrame = st.GetFrame(frameIndex); string methodName = "", className = "", fileName = "", lineNumber = ""; if (locationFrame != null) { System.Reflection.MethodBase method = locationFrame.GetMethod(); if (method != null) { methodName = method.Name; if (method.DeclaringType != null) { className = method.DeclaringType.FullName; } } fileName = System.IO.Path.GetFileName(locationFrame.GetFileName()); lineNumber = locationFrame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo); // Combine all location info fullInfo += className + '.' + methodName + '(' + fileName + ':' + lineNumber + "):\n"; } if (methodName.ToUpper().Equals("MAIN")) { break; } ++frameIndex; } //outMsg += fullInfo + "::"; } outMsg += msg + "\n" + fullInfo; if ((m_logOutput & LoggerOutput.CONSOLE) == LoggerOutput.CONSOLE) { //display to the console Console.WriteLine(outMsg); } if ((m_logOutput & LoggerOutput.FILE) == LoggerOutput.FILE) { //write to file (append) System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(m_outputFile, (!m_overriteLogFlag)); fileWriter.WriteLine(outMsg); fileWriter.Flush(); fileWriter.Close(); } }
/// <summary> /// Write an error message to the desired destination /// </summary> /// <param name="msg">The message to be written</param> /// <param name="erLevel">The error level of the message</param> public static void Write(string msg, LoggerErrorLevels erLevel) { if ((!m_showDebugInfo) && (erLevel == LoggerErrorLevels.DEBUG)) return; string outMsg = DateTime.Now.ToShortDateString() + " "+DateTime.Now.ToLongTimeString(); switch (erLevel) { case LoggerErrorLevels.INFO: { outMsg += " INFO: "; break; } case LoggerErrorLevels.WARNING: { outMsg += " WARNING: "; break; } case LoggerErrorLevels.ERROR: { outMsg += " ERROR: "; break; } case LoggerErrorLevels.FATAL: { outMsg += " FATAL: "; break; } case LoggerErrorLevels.DEBUG: { outMsg += " DEBUG: "; break; } } string fullInfo = ""; if (erLevel != LoggerErrorLevels.INFO){ StackTrace st = new StackTrace(true); int frameIndex = 0; // skip frames not from fqnOfCallingClass while (frameIndex < st.FrameCount) { StackFrame frame = st.GetFrame(frameIndex); if (frame != null && !frame.GetMethod().DeclaringType.Name.Equals("Logger")) { break; } ++frameIndex; } //++frameIndex; while (frameIndex < st.FrameCount) { // now frameIndex is the first 'user' caller frame StackFrame locationFrame = st.GetFrame(frameIndex); string methodName = "", className = "", fileName = "", lineNumber = ""; if (locationFrame != null) { System.Reflection.MethodBase method = locationFrame.GetMethod(); if (method != null) { methodName = method.Name; if (method.DeclaringType != null) { className = method.DeclaringType.FullName; } } fileName = System.IO.Path.GetFileName(locationFrame.GetFileName()); lineNumber = locationFrame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo); // Combine all location info fullInfo += className + '.' + methodName + '(' + fileName + ':' + lineNumber + "):\n"; } if (methodName.ToUpper().Equals("MAIN")) break; ++frameIndex; } //outMsg += fullInfo + "::"; } outMsg += msg+"\n"+fullInfo; if ((m_logOutput & LoggerOutput.CONSOLE )== LoggerOutput.CONSOLE) { //display to the console Console.WriteLine(outMsg); } if ((m_logOutput & LoggerOutput.FILE) == LoggerOutput.FILE) { //write to file (append) System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(m_outputFile,(!m_overriteLogFlag)); fileWriter.WriteLine(outMsg); fileWriter.Flush(); fileWriter.Close(); } }