public static LogFile getAltLog(string dir, string logname) { LogFile logf = null; try { lock (alocker) { foreach (LogFile l in logfiles) { if (l.fileName == logname) return l; } } } catch (Exception) { if (logfiles == null) logfiles = new List<LogFile>(); } finally { logf = new LogFile(); if (!dir.EndsWith("\\")) dir += "\\"; logf.logDir = string.Format("{0}{1}", dir, logf.logDir); logf.fileName = logname; lock (alocker) { logfiles.Add(logf); } } return logf; }
public IridiumToMySQL() { string dir = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); log = LogFile.getLog(dir); log.DirectoryType = DirFileType.YearMonthDir; LogFile.MaxLogsInQueue = 10; log.FileSize = 1024; config = ConfigFile.getConfigFile(); md = new MailsDatabase(dir); }
public static void logRelevantInformation(LogFile log, Dictionary<string, Type> extraTypes) { if (log != null) { // Get the operating system version. OperatingSystem os = Environment.OSVersion; log.writeToLogFile(LogType.LOG_CONFIG, "Config", "Server {0}", os.VersionString); Version ver = Environment.Version; log.writeToLogFile(LogType.LOG_CONFIG, "Config", "CLR Version {0}", ver.ToString()); // Get the version of the main assembly. AssemblyName assemName = Assembly.GetEntryAssembly().GetName(); log.writeToLogFile(LogType.LOG_CONFIG, "Config", "Main Application {0}, Version {1}", assemName.Name, assemName.Version.ToString()); // Get the version of the GeneralUtilities assembly. assemName = Assembly.GetAssembly(log.GetType()).GetName(); log.writeToLogFile(LogType.LOG_CONFIG, "Config", "{0} dll, Version {1}", assemName.Name, assemName.Version.ToString()); foreach (KeyValuePair<string, Type> t in extraTypes) { // Get the version of the this Terminal Server assembly. assemName = Assembly.GetAssembly(t.Value).GetName(); log.writeToLogFile(LogType.LOG_CONFIG, "Config", "{0} {1}, Version {2}", assemName.Name, t.Key, assemName.Version.ToString()); } } }
public static void LogProcessInformation(LogFile log) { Process proc = Process.GetCurrentProcess(); try { string str = ""; int count = proc.Threads.Count; int count2 = (proc.Threads).OfType<System.Diagnostics.ProcessThread>().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).Count(); str = string.Format(" {{ \"ProcessInformation\" : {{\"Cantidad de Threads\" : \"{0}\", \"ProcessThread Running\": \"{1}\" ", count, count2); str = string.Format("{0}, \"WorkingSet64\": \"{1}\" ", str, ((float)proc.WorkingSet64 / 1024 / 1024).ToString("#.####")); str = string.Format("{0}, \"PagedMemorySize64\": \"{1}\" ", str, ((float)proc.PagedMemorySize64 / 1024 / 1024).ToString("#.####")); str = string.Format("{0}, \"VirtualMemorySize64\" : \"{1}\" ", str, ((float)proc.VirtualMemorySize64 / 1024 / 1024).ToString("#.####")); str = string.Format("{0}, \"UserProcessorTime\" : \"{1}\" ", str, proc.UserProcessorTime); str = string.Format("{0}, \"TotalProcessorTime\" : \"{1}\" }} }}", str, proc.TotalProcessorTime); log.writeToLogFile(LogType.LOG_ALERT, "LogProcessInformation", str); } catch (Exception ex) { log.writeToLogFile(LogType.LOG_ALERT, "LogProcessInformation", "Error processing information: {0}", ex.Message); } proc.Dispose(); proc = null; log.Flush(); }
protected void writeToFile() { if (working) return; bool hasFile = false; StreamWriter swLog; List<string> localLogs = new List<string>(); string log = ""; //try to get the file if (!File.Exists(LogFileName)) { try { swLog = new StreamWriter(LogFileName, true); hasFile = true; } catch (Exception ex) { lock (locker) logs.Add(getFormattedLog(string.Format("ha habido un error al abrir el archivo: {0}", ex.Message))); try { hasFile = OpenLogFileFromThread(out swLog); } catch (Exception) { working = false; return; } } } else { try { swLog = File.AppendText(LogFileName); hasFile = true; } catch (Exception ex) { if (WriteFileExceptions) { lock (locker) logs.Add(getFormattedLog(string.Format("No se puede abrir el archivo para agregar, mensaje: {0}, {1}", ex.Data, ex.Message))); } try { hasFile = OpenLogFileFromThread(out swLog); } catch (Exception) { working = false; return; } } } if (hasFile && swLog != null) { //we get the file already lock (locker) { working = true; for (int i = logs.Count - 1; i >= 0; i--) { log = logs[0]; try { if ((maxLineLength > 0) && (log.Length > maxLineLength)) localLogs.Add(log.Substring(0, maxLineLength)); else localLogs.Add(log); logs.RemoveAt(0); } catch (Exception ex) { lock (locker) logs.Add(getFormattedLog(string.Format("Error escribiendo el archivo, mensaje: {0}", ex.Message))); } } } List<string> toDelete = new List<string>(); try { foreach (string l in localLogs) { swLog.WriteLine(l); toDelete.Add(l); } } catch (Exception ex) { lock (locker) logs.Add(getFormattedLog(string.Format("Error escribiendo el archivo, mensaje: {0}", ex.Message))); foreach (string l in toDelete) localLogs.Remove(l); lock (locker) { foreach (string l in localLogs) logs.Insert(0, l); } return; } finally { swLog.Close(); working = false; } } //check the size of the file if (fileSize > 0) { try { FileInfo f = new FileInfo(LogFileName); if (f.Length > fileSize) logFileNumber++; } catch (Exception) { // ? do nothing } } }
protected static int checkLogFileNumber(LogFile log) { int max = 0; string localLogDir = log.logDir; switch (log.dirType) { case DirFileType.YearMonthDir: if (log.logDir.EndsWith("\\")) localLogDir = string.Format("{0}{1}\\{2}", log.logDir, DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM")); else if (log.logDir.EndsWith(new string(Convert.ToChar(0x5C), 1))) localLogDir = string.Format("{0}\\{1}\\{2}", log.logDir.Remove(log.logDir.Length - 1), DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM")); else localLogDir = string.Format("{0}\\{1}\\{2}", log.logDir, DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM")); break; case DirFileType.YearMonthDayDir: if (log.logDir.EndsWith("\\")) localLogDir = string.Format("{0}{1}\\{2}\\{3}", log.logDir, DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); else if (log.logDir.EndsWith(new string(Convert.ToChar(0x5C), 1))) localLogDir = string.Format("{0}\\{1}\\{2}\\{3}", log.logDir.Remove(log.logDir.Length - 1), DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); else localLogDir = string.Format("{0}\\{1}\\{2}\\{3}", log.logDir, DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); break; } //look for prior log files string[] files = new string[0]; try { files = Directory.GetFiles(localLogDir, string.Format("{0}{1}.*.{2}", log.LogRootFileName, DateTime.Now.ToString("yyyyMMdd"), log.logDefaultExtension)); } catch (Exception) { } string[] look; int tmax = 0; foreach (string s in files) { look = s.Split('.'); if (look.Count() == 3) { try { tmax = int.Parse(look[1]); if (tmax > max) max = tmax; } catch (Exception) { /* do nothing */ } } } log.logFileNumber = max; return max; }
public static LogFile getLog() { try { string name = log.LogRootFileName; } catch (Exception) { log = new LogFile(); } checkLogDir(log.logDir); checkLogFileNumber(log); return log; }
public static LogFile getLog(string dir) { try { string name = log.LogRootFileName; } catch (Exception) { log = new LogFile(); if (!dir.EndsWith("\\")) dir += "\\"; log.logDir = string.Format("{0}{1}", dir, log.logDir); } checkLogDir(System.IO.Path.GetDirectoryName(log.LogFileName)); //checkLogDir(log.logDir); checkLogFileNumber(log); return log; }
public void Dispose() { config = null; log = null; }
public static bool sendReport(UdpClient client, IPEndPoint to, Byte[] msg, int timeOut, int retries, LogFile log) { Byte[] received = new Byte[msg.Count()]; int loop = 0; int nBytesSent; String dataReceived; string strRecv; bool response = false; do { try { nBytesSent = client.Send(msg, msg.Length); log.writeToLogFile(string.Format("message sent: {0}", System.Text.ASCIIEncoding.ASCII.GetString(msg))); } catch (Exception ex) { log.writeToLogFile("Excepcion al enviar udp : {0}", ex.Message); } Thread.Sleep(250); client.Client.ReceiveTimeout = timeOut * 2; for (int i = 0; i < 5; i++) { if (client.Available > 0) { try { received = client.Receive(ref to); dataReceived = System.Text.Encoding.ASCII.GetString(received); strRecv = System.Text.Encoding.ASCII.GetString(received).Trim('\0'); log.writeToLogFile(string.Format("message response: {0}", strRecv)); response = true; break; } catch (Exception ex) { log.writeToLogFile("Excepcion al recibir udp : {0}", ex.Message); } } else { //si ya lo envió, dale mas tiempo al server log.writeToLogFile("Extra time para recibir udp : {0}", timeOut * (loop + 1)); Thread.Sleep(timeOut * (loop + 1)); } } loop++; } while (!response && loop < retries); return response; }