33 namespace OpenSim.Framework
45 public bool Enabled {
get;
private set; }
47 private string m_logDirectory =
".";
48 private int m_logMaxFileTimeMin = 5;
49 public String LogFileHeader {
get; set; }
51 private StreamWriter m_logFile = null;
52 private TimeSpan m_logFileLife;
53 private DateTime m_logFileEndTime;
54 private Object m_logFileWriteLock =
new Object();
55 private bool m_flushWrite;
58 public ILog ErrorLogger = null;
59 private string LogHeader =
"[LOG WRITER]";
79 public LogWriter(
string dir,
string headr,
int maxFileTime,
bool flushWrite)
81 m_logDirectory = dir == null ?
"." : dir;
83 LogFileHeader = headr == null ?
"log-" : headr;
85 m_logMaxFileTimeMin = maxFileTime;
86 if (m_logMaxFileTimeMin < 1)
87 m_logMaxFileTimeMin = 5;
89 m_logFileLife =
new TimeSpan(0, m_logMaxFileTimeMin, 0);
90 m_logFileEndTime = DateTime.Now + m_logFileLife;
92 m_flushWrite = flushWrite;
97 public LogWriter(
string dir,
string headr,
int maxFileTime) : this(dir, headr, maxFileTime, false)
109 if (m_logFile != null)
117 public void Write(
string line, params
object[] args)
119 if (!Enabled)
return;
120 Write(String.Format(line, args));
125 if (!Enabled)
return;
126 if (m_logFile != null)
134 if (!Enabled)
return;
137 lock (m_logFileWriteLock)
139 DateTime now = DateTime.UtcNow;
140 if (m_logFile == null || now > m_logFileEndTime)
142 if (m_logFile != null)
150 m_logFileEndTime = now + m_logFileLife;
151 string path = (m_logDirectory.Length > 0 ? m_logDirectory
152 + System.IO.Path.DirectorySeparatorChar.ToString() :
"")
153 + String.Format(
"{0}{1}.log", LogFileHeader, now.ToString(
"yyyyMMddHHmmss"));
154 m_logFile =
new StreamWriter(
File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite));
156 if (m_logFile != null)
158 StringBuilder buff =
new StringBuilder(line.Length + 25);
159 buff.Append(now.ToString(
"yyyyMMddHHmmssfff"));
164 m_logFile.Write(buff.ToString());
172 if (ErrorLogger != null)
174 ErrorLogger.ErrorFormat(
"{0}: FAILURE WRITING TO LOGFILE: {1}", LogHeader, e);
Class for writing a high performance, high volume log file. Sometimes, to debug, one has a high volum...
LogWriter(string dir, string headr, int maxFileTime, bool flushWrite)
Create a log writer instance.
LogWriter(string dir, string headr, int maxFileTime)
LogWriter()
Create a log writer that will not write anything. Good for when not enabled but the write statements ...
void Write(string line, params object[] args)