Support for creating log path and having no log file

This commit is contained in:
2014-05-16 14:13:39 -04:00
parent 91dcbf5116
commit 83f6aaaf61

View File

@@ -63,31 +63,37 @@ namespace Common.Debug
{ {
_echoToConsole = echoToConsole; _echoToConsole = echoToConsole;
// Use the file name template build the base file name if (!Directory.Exists(logPath))
_mainFileName = string.Format(_fileNameTemplate, rootName, uniqueId); Directory.CreateDirectory(logPath);
// Get the list of old log files if (!string.IsNullOrEmpty(logPath))
string[] oldLogFiles = Directory.GetFiles(logPath, string.Format(_fileNameTemplate, rootName, "*"), SearchOption.TopDirectoryOnly);
// Sort the list by creation date
Array.Sort(oldLogFiles, new FileCreationTimeComparer());
// Keep only the last X revisions
for (int i = _keepRevisions; i < oldLogFiles.Length; i++)
{ {
// Delete the file // Use the file name template build the base file name
File.Delete(oldLogFiles[i]); _mainFileName = string.Format(_fileNameTemplate, rootName, uniqueId);
// Get the list of old log files
string[] oldLogFiles = Directory.GetFiles(logPath, string.Format(_fileNameTemplate, rootName, "*"), SearchOption.TopDirectoryOnly);
// Sort the list by creation date
Array.Sort(oldLogFiles, new FileCreationTimeComparer());
// Keep only the last X revisions
for (int i = _keepRevisions; i < oldLogFiles.Length; i++)
{
// Delete the file
File.Delete(oldLogFiles[i]);
}
// Add the log path
_mainFileName = Path.Combine(logPath, _mainFileName);
// Create the listener
_traceListener = new TextWriterTraceListener(_mainFileName);
// Setup the debug listener
Trace.Listeners.Add(_traceListener);
} }
// Add the log path
_mainFileName = Path.Combine(logPath, _mainFileName);
// Create the listener
_traceListener = new TextWriterTraceListener(_mainFileName);
// Setup the debug listener
Trace.Listeners.Add(_traceListener);
_initialized = true; _initialized = true;
WriteLine("Application starting"); WriteLine("Application starting");
@@ -103,13 +109,16 @@ namespace Common.Debug
// Flush the trace // Flush the trace
Trace.Flush(); Trace.Flush();
// Remove the listener if (_traceListener != null)
Trace.Listeners.Remove(_traceListener); {
// Remove the listener
Trace.Listeners.Remove(_traceListener);
// Close the listener // Close the listener
_traceListener.Close(); _traceListener.Close();
_traceListener.Dispose(); _traceListener.Dispose();
_traceListener = null; _traceListener = null;
}
// Close the trace // Close the trace
Trace.Close(); Trace.Close();