--- RollingFileAppender.cs.old 2005-02-07 22:33:34.000000000 +1100
+++ RollingFileAppender.cs 2006-01-17 16:23:13.788267200 +1100
@@ -453,6 +453,26 @@
set { m_staticLogFileName = value; }
}
+ ///
+ /// Gets or sets a value indicating whether to preserve the file name extension when rolling.
+ ///
+ ///
+ /// true if the file name extension should be preserved.
+ ///
+ ///
+ ///
+ /// By default file.log is rolled to file.log.yyyy-mm-dd or file.log.curSizeRollBackup.
+ /// However, under Windows the new file name will loose any program associations as the
+ /// extension is changed. Optionally file.log can be renamed to file.yyyy-mm-dd.log or
+ /// file.curSizeRollBackup.log to maintain any program associations.
+ ///
+ ///
+ public bool PreserveLogFileNameExtension
+ {
+ get { return m_preserveLogFileNameExtension; }
+ set { m_preserveLogFileNameExtension = value; }
+ }
+
#endregion Public Instance Properties
#region Override implementation of FileAppender
@@ -973,6 +993,19 @@
#region Roll File
+ private string CombinePath(string path1, string path2)
+ {
+ string extension = Path.GetExtension(path1);
+ if (m_preserveLogFileNameExtension && extension.Length > 0)
+ {
+ return Path.GetFileNameWithoutExtension(path1) + path2 + extension;
+ }
+ else
+ {
+ return path1 + path2;
+ }
+ }
+
///
/// Rollover the file(s) to date/time tagged file(s).
///
@@ -999,12 +1032,12 @@
//something has gone wrong if we hit this -- we should only
//roll over if the new file will be different from the old
string dateFormat = m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo);
- if (m_scheduledFilename.Equals(File + dateFormat))
+ if (m_scheduledFilename.Equals(CombinePath(File, dateFormat)))
{
- ErrorHandler.Error("Compare " + m_scheduledFilename + " : " + File + dateFormat);
+ ErrorHandler.Error("Compare " + m_scheduledFilename + " : " + CombinePath(File, dateFormat));
return;
}
-
+
if (fileIsOpen)
{
// close current file, and rename it to datedFilename
@@ -1014,8 +1047,8 @@
//we may have to roll over a large number of backups here
for (int i = 1; i <= m_curSizeRollBackups; i++)
{
- string from = File + '.' + i;
- string to = m_scheduledFilename + '.' + i;
+ string from = CombinePath(File, "." + i);
+ string to = CombinePath(m_scheduledFilename, "." + i);
RollFile(from, to);
}
@@ -1026,7 +1059,7 @@
m_curSizeRollBackups = 0;
//new scheduled name
- m_scheduledFilename = File + m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo);
+ m_scheduledFilename = CombinePath(File, m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo));
if (fileIsOpen)
{
@@ -1230,20 +1263,20 @@
// Delete the oldest file, to keep Windows happy.
if (m_curSizeRollBackups == m_maxSizeRollBackups)
{
- DeleteFile(baseFileName + '.' + m_maxSizeRollBackups);
+ DeleteFile(CombinePath(baseFileName , "." + m_maxSizeRollBackups));
m_curSizeRollBackups--;
}
// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
for (int i = m_curSizeRollBackups; i >= 1; i--)
{
- RollFile((baseFileName + "." + i), (baseFileName + '.' + (i + 1)));
+ RollFile((CombinePath(baseFileName, "." + i)), (CombinePath(baseFileName, "." + (i + 1))));
}
m_curSizeRollBackups++;
// Rename fileName to fileName.1
- RollFile(baseFileName, baseFileName + ".1");
+ RollFile(baseFileName, CombinePath(baseFileName, ".1"));
}
else
{
@@ -1252,13 +1285,13 @@
{
//delete the first and keep counting up.
int oldestFileIndex = m_curSizeRollBackups - m_maxSizeRollBackups + 1;
- DeleteFile(baseFileName + '.' + oldestFileIndex);
+ DeleteFile(CombinePath(baseFileName, "." + oldestFileIndex));
}
if (m_staticLogFileName)
{
m_curSizeRollBackups++;
- RollFile(baseFileName, baseFileName + '.' + m_curSizeRollBackups);
+ RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
}
@@ -1423,6 +1456,11 @@
private bool m_staticLogFileName = true;
///
+ /// Value indicating whether to preserve the file name extension when rolling.
+ ///
+ private bool m_preserveLogFileNameExtension = false;
+
+ ///
/// FileName provided in configuration. Used for rolling properly
///
private string m_baseFileName;