public static LogFile0 getLog(string dir) { try { string name = log.logRootFileName; } catch (Exception) { log = new LogFile0(); if (!dir.EndsWith("\\")) dir += "\\"; log.logDir = string.Format("{0}{1}", dir, log.logDir); } checkLogDir(log.logDir); checkLogFileNumber(log); return log; }
public static LogFile0 getLog() { try { string name = log.logRootFileName; } catch (Exception) { log = new LogFile0(); } checkLogDir(log.logDir); checkLogFileNumber(log); return log; }
protected static int checkLogFileNumber(LogFile0 log) { int max = 0; //look for prior log files string[] files = Directory.GetFiles(log.logDir, string.Format("{0}{1}.*.{2}", log.logRootFileName, DateTime.Now.ToString("yyyyMMdd"), log.logDefaultExtension)); 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; }
protected void writeToFile() { if (working) return; StreamWriter swLog; string log = ""; if (!File.Exists(LogFileName)) { try { swLog = new StreamWriter(LogFileName, true); } catch (Exception ex) { logs.Add(getFormattedLog(string.Format("ha habido un error al abrir el archivo: {0}",ex.Message))); swLog = OpenLogFileFromThread(); } } else { try { swLog = File.AppendText(LogFileName); } catch (Exception ex) { logs.Add(getFormattedLog(string.Format("No se puede abrir el archivo para agregar, mensaje: {0}, {1}", ex.Data, ex.Message))); swLog = OpenLogFileFromThread(); } } //we get the file already lock (logs) { working = true; for (int i = logs.Count - 1; i >= 0; i--) { log = logs[0]; try { if ((maxLineLength > 0) && (log.Length > maxLineLength)) swLog.WriteLine(log.Substring(0, maxLineLength)); else swLog.WriteLine(log); logs.RemoveAt(0); } catch (Exception ex) { logs.Add(getFormattedLog(string.Format("Error escribiendo el archivo, mensaje: {0}", ex.Message))); swLog.Close(); working = false; return; } } } 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 } } }
public static LogFile0 getAltLog(string dir, string logname) { LogFile0 logf = null; try { foreach (LogFile0 l in alts) { if (l.altName == logname) return l; } } catch (Exception) { if (alts == null) alts = new List<LogFile0>(); } finally { logf = new LogFile0(); if (!dir.EndsWith("\\")) dir += "\\"; logf.logDir = string.Format("{0}{1}", dir, logf.logDir); logf.logRootFileName = string.Format("{0}_", logname); alts.Add(logf); } checkLogDir(logf.logDir); checkLogFileNumber(logf); return logf; }