// --------------------------------------------------------------------- // Creates a streamwriter that will write to the given file name. // --------------------------------------------------------------------- public LogClient requestLog(string filename) { StreamWriter sw; filename = LogTools.safeFileName(filename); string filePath = m_logPath + filename; try { if (m_logStreamwriters.ContainsKey(filename)) { sw = m_logStreamwriters[filename]; } else { FileStream fs = new FileStream(filePath, FileMode.Append); sw = new StreamWriter(fs); m_logStreamwriters.TryAdd(filename, sw); //check for dir and create if necessary //todo } } catch (IOException e) { App.AppEventLog.WriteEntry(LogTools.getExceptionString(m_threadName, "requestLog", e)); throw e; } LogClient lc = new LogClient(filename, this); return(lc); //return sw; }
/// <summary> /// Archives a log file by copying its contents to a compressed archive and /// deleting old file. /// </summary> /// <param name="fileName">the name of the file to compress</param> private void processArchive(String fileName) { //compress file try { String outFileName = fileName.Substring(0, fileName.Length - 4); //remove ".log" from end of name outFileName += "-" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".log.gz"; FileStream inFile = File.OpenRead(fileName); FileStream outFile = File.Create(outFileName); GZipStream compress = new GZipStream(outFile, CompressionMode.Compress); App.AppEventLog.WriteEntry("File is being archived: " + outFileName); //copy contents inFile.CopyTo(compress); compress.Close(); inFile.Close(); outFile.Close(); File.Delete(fileName); } catch (Exception e) { App.AppEventLog.WriteEntry(LogTools.getExceptionString(m_threadName, "processArchive", e, "Could not find filename " + fileName + "")); throw; } }
/// <summary> /// Generates telegram object from delimited string. /// Expected fields are (char kiosk type)(int kiosk id)(KioskMsgType message type)(base64 string encrypted data) /// </summary> /// <param name="telegram">'RS' delimited 4-field string</param> public vkTelegram(string telegram) { string[] fields; try { fields = telegram.Split(m_delim); if (fields.Length != 4) { Exception e = new Exception("Telegram contains an invalid number of fields: " + fields.Length); App.AppEventLog.WriteEntry(LogTools.getExceptionString("vkTelegram", "vkTelegram", e)); throw e; } m_kioskty = fields[0][0]; m_kioskid = fields[1]; m_msgty = (KioskMsgType)Enum.Parse(typeof(KioskMsgType), fields[2]); switch (m_msgty) { case KioskMsgType.alt_card_string: case KioskMsgType.card_string: case KioskMsgType.new_CC: case KioskMsgType.dl_m_cardswipe: case KioskMsgType.monthlyCC: case KioskMsgType.use_dif_CC: if (fields[3].Length > 0) { m_data = m_crypt.decryptAES(fields[3]); } else { m_data = fields[3]; } break; default: m_data = fields[3]; break; } //m_data = m_crypt.decryptAES(fields[3]); } catch (Exception e) { //todo: add validation (see above), error handling App.AppEventLog.WriteEntry(LogTools.getExceptionString("vkTelegram", "vkTelegram", e)); throw e; } }
/// <summary> /// assembles telegram string by delimiting fields /// </summary> /// <returns>telegram string</returns> public override string ToString() { string telegram = ""; try { telegram += m_kioskty; telegram += m_delim[0]; telegram += m_kioskid; telegram += m_delim[0]; telegram += m_msgty.ToString(); telegram += m_delim[0]; switch (m_msgty) { case KioskMsgType.alt_card_string: case KioskMsgType.card_string: case KioskMsgType.new_CC: case KioskMsgType.dl_m_cardswipe: case KioskMsgType.use_dif_CC: case KioskMsgType.monthlyCC: if (m_data.Length > 0) { telegram += m_crypt.encryptAES(m_data); } else { telegram += m_data; } break; default: telegram += m_data; break; } //telegram += m_crypt.encryptAES(m_data); } catch (Exception e) { //todo: exception handling App.AppEventLog.WriteEntry(LogTools.getExceptionString("vkTelegram", "ToString", e)); throw e; } return(telegram); }
public static string safeDirectoryName(string dirName) { //App.AppEventLog.WriteEntry("Debug: sanitizing " + dirName); //look for single slash, convert to double //todo //look for unix /, convert to win \ //todo? //look for win \, convert to unix / //todo? //ensure that directory ends in path delimiter if (!dirName.EndsWith("" + Path.DirectorySeparatorChar)) { dirName += Path.DirectorySeparatorChar; } //look for other illegal chars foreach (char c in Path.GetInvalidPathChars()) { dirName = dirName.Replace(c, '_'); } //check for directory / try and create try { if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } } catch (IOException e) { App.AppEventLog.WriteEntry(LogTools.getExceptionString("Log", "safeDirectoryName", e)); } return(dirName); }
public void threadLoop() { try { Thread.CurrentThread.Name = m_threadName; // App.AppEventLog.WriteEntry("LogBook Thread is starting"); while (!m_exit) { //write pending messages from the queue if (m_logQueue.Count > LOGBUFFER || (DateTime.Now - m_lastLogged) >= m_logInterval) { List <string> toBeFlushed = new List <string>(); //grab current state of Queue (let successive enqueues pile up until next cycle) int count = m_logQueue.Count; for (int i = 0; i < count; i++) { LogTupple toBeLogged; bool success = m_logQueue.TryDequeue(out toBeLogged); if (!success) { continue;//skip for now } if (!toBeFlushed.Contains(toBeLogged.fileName)) { toBeFlushed.Add(toBeLogged.fileName); } if (m_logStreamwriters.ContainsKey(toBeLogged.fileName)) { if (!String.IsNullOrEmpty(toBeLogged.msg)) { m_logStreamwriters[toBeLogged.fileName].WriteLine(toBeLogged.msg); } } else { App.AppEventLog.WriteEntry(LogTools.getStatusString(m_threadName, "threadloop", "Filename = " + toBeLogged.fileName)); } } foreach (string fn in toBeFlushed) { m_logStreamwriters[fn].Flush(); } m_lastLogged = DateTime.Now; } //handle archival and deletion of old logs if ((DateTime.Now - m_lastArchive) >= m_logArchiveIntv) { List <string> toBeArchived = new List <string>(); foreach (KeyValuePair <string, StreamWriter> kvp in m_logStreamwriters) { if (kvp.Value.BaseStream is FileStream) { FileInfo file = new FileInfo(m_logPath + kvp.Key); if (file.Length > m_maxFileSize) { //close the stream so we can manipulate file kvp.Value.Close(); toBeArchived.Add(m_logPath + kvp.Key); } } } //cannot remove from inside the m_logStreamwriters foreach foreach (string fileName in toBeArchived) { StreamWriter sw; m_logStreamwriters.TryRemove(Path.GetFileName(fileName), out sw); processArchive(fileName); //recreate logfile FileStream fs = new FileStream(fileName, FileMode.Append); m_logStreamwriters.TryAdd(Path.GetFileName(fileName), new StreamWriter(fs)); } m_lastArchive = DateTime.Now; cleanupLogs(); } Thread.Sleep(10); } } catch (ThreadAbortException ex) { App.AppEventLog.WriteEntry(LogTools.getExceptionString(m_threadName, "threadloop", ex, "LogBook Thread is stopping")); //todo: rethrow? retry? throw ex; } finally { foreach (KeyValuePair <string, StreamWriter> kvp in m_logStreamwriters) { kvp.Value.Close(); } } }