private static string _formatLogMSG(SAFLOGGER.LOGGEREventID loggerEventID, string appBASEid, string[] appMSGtext) { Process _thisProc = null; string result; try { _thisProc = Process.GetCurrentProcess(); result = string.Format("{0} [{1}] [{2}]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), loggerEventID.ToString(), _thisProc.ProcessName) + " " + string.Join(" ", appMSGtext) + ((loggerEventID == SAFLOGGER.LOGGEREventID.EXCEPTION) ? string.Concat(new string[] { Environment.NewLine, SAFLOGGER._getMachineInfo(2)[0], Environment.NewLine, SAFLOGGER._getMachineInfo(2)[1], Environment.NewLine }) : ""); } catch { result = "... [ unable to format message ] ..."; } finally { if (_thisProc != null) { _thisProc.Dispose(); } _thisProc = null; } return(result); }
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!" })); } }