public static void Write(SAFLOGGER.LOGGEREventID loggerEventID, string appBASEid, string[] appMSGtext) { try { int _SAFDEPLOYTRACELevel; if (-9 == (_SAFDEPLOYTRACELevel = int.Parse("0" + ConfigurationManager.AppSettings.Get("SAFTRACE_LEVEL")))) { Debug.Write(SAFLOGGER._formatLogMSG(loggerEventID, appBASEid, appMSGtext)); if (loggerEventID != (SAFLOGGER.LOGGEREventID)(-1)) { return; } } if (loggerEventID <= (SAFLOGGER.LOGGEREventID)_SAFDEPLOYTRACELevel) { SAFLOGGER.writeToFile(appBASEid, SAFLOGGER._formatLogMSG(loggerEventID, appBASEid, appMSGtext)); } } catch (Exception ex) { SAFLOGGER.writeToEventLog(EventLogEntryType.Error, SAFLOGGER._formatLogMSG(SAFLOGGER.LOGGEREventID.EXCEPTION, "http://sfexpandsecure.logger.softfinanca.com/", new string[] { ex.ToString(), "unable to dump message log!!" }) + Environment.NewLine + SAFLOGGER._formatLogMSG(loggerEventID, appBASEid, appMSGtext)); } }
private static void writeToFile(string fileBaseName, string _logMessage) { int _filelockDelay = 0; string _baseFilePath = null; try { string _fileFormater; if (null == (_fileFormater = ConfigurationManager.AppSettings.Get("SAFTRACE_FILENAMEFORMATER"))) { _fileFormater = "yyyyMMddHH"; } string _filePathName; if (null == (_filePathName = ConfigurationManager.AppSettings.Get("SAFTRACE_FILEPATH"))) { _filePathName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } _baseFilePath = Path.Combine(_filePathName, string.Format(fileBaseName.Trim() + "{0}.LOG", DateTime.Now.ToString(_fileFormater))); int.TryParse("0" + ConfigurationManager.AppSettings.Get("SAFTRACE_FILELOCKDELAY"), out _filelockDelay); } catch (Exception ex) { Debug.Write(SAFLOGGER._formatLogMSG(SAFLOGGER.LOGGEREventID.WARNING, "http://sfexpandsecure.logger.softfinanca.com/", new string[] { "unable to write message!", ex.ToString() })); Debug.Write(_logMessage); return; } if (Monitor.TryEnter(SAFLOGGER.s_Lock, (_filelockDelay < 1000) ? 1000 : _filelockDelay)) { try { object obj; Monitor.Enter(obj = SAFLOGGER.s_Lock); try { if (SAFLOGGER.s_Writer == null) { if (!File.Exists(_baseFilePath)) { SAFLOGGER.s_Writer = File.CreateText(_baseFilePath); string[] _fileHeaders = SAFLOGGER._getMachineInfo(1); SAFLOGGER.s_Writer.WriteLine(_fileHeaders[0] + Environment.NewLine + _fileHeaders[1] + Environment.NewLine); } else { SAFLOGGER.s_Writer = File.AppendText(_baseFilePath); } } SAFLOGGER.s_Writer.WriteLine(_logMessage); SAFLOGGER.s_Writer.Flush(); SAFLOGGER.s_Writer.Close(); } finally { Monitor.Exit(obj); } } catch (Exception ex) { Debug.Write(SAFLOGGER._formatLogMSG(SAFLOGGER.LOGGEREventID.WARNING, "http://sfexpandsecure.logger.softfinanca.com/", new string[] { "unable to write message!", ex.ToString() })); Debug.Write(_logMessage); } finally { SAFLOGGER.s_Writer = null; Monitor.Exit(SAFLOGGER.s_Lock); } } else { SAFLOGGER.writeToEventLog(EventLogEntryType.Error, SAFLOGGER._formatLogMSG(SAFLOGGER.LOGGEREventID.EXCEPTION, "http://sfexpandsecure.logger.softfinanca.com/", new string[] { "Timeout; could not obtain lock to log file!" }) + Environment.NewLine + _logMessage); Debug.Write(SAFLOGGER._formatLogMSG(SAFLOGGER.LOGGEREventID.WARNING, "http://sfexpandsecure.logger.softfinanca.com/", new string[] { "Timeout; could not obtain lock to log file!" })); } }