Index: RollingFileAppender.cs
===================================================================
--- RollingFileAppender.cs (revision 607768)
+++ RollingFileAppender.cs (working copy)
@@ -452,7 +452,27 @@
}
}
}
-
+
+ ///
+ /// 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; }
+ }
+
///
/// Gets or sets a value indicating whether to always log to
/// the same file.
@@ -650,12 +670,12 @@
if (m_rollDate)
{
- fileName = fileName + m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo);
+ fileName = CombinePath(fileName, m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo));
}
if (m_countDirection >= 0)
{
- fileName = fileName + '.' + m_curSizeRollBackups;
+ fileName = CombinePath(fileName, "." + m_curSizeRollBackups);
}
}
@@ -694,9 +714,16 @@
///
///
///
- private static string GetWildcardPatternForFile(string baseFileName)
+ private string GetWildcardPatternForFile(string baseFileName)
{
- return baseFileName + '*';
+ if (m_preserveLogFileNameExtension)
+ {
+ return Path.GetFileNameWithoutExtension(baseFileName) + ".*" + Path.GetExtension(baseFileName);
+ }
+ else
+ {
+ return baseFileName + '*';
+ }
}
///
@@ -727,7 +754,7 @@
for (int i = 0; i < files.Length; i++)
{
string curFileName = Path.GetFileName(files[i]);
- if (curFileName.StartsWith(baseFileName))
+ if (curFileName.StartsWith(Path.GetFileNameWithoutExtension(baseFileName)))
{
alFiles.Add(curFileName);
}
@@ -822,7 +849,7 @@
///
private void InitializeFromOneFile(string baseFile, string curFileName)
{
- if (! curFileName.StartsWith(baseFile) )
+ if (curFileName.StartsWith(Path.GetFileNameWithoutExtension(baseFile)) == false)
{
// This is not a log file, so ignore
return;
@@ -833,13 +860,7 @@
return;
}
- int index = curFileName.LastIndexOf(".");
- if (-1 == index)
- {
- // This is not an incremented logfile (.1 or .2)
- return;
- }
-
+ /*
if (m_staticLogFileName)
{
int endLength = curFileName.Length - index;
@@ -849,6 +870,7 @@
return;
}
}
+ */
// Only look for files in the current roll point
if (m_rollDate && !m_staticLogFileName)
@@ -863,40 +885,38 @@
try
{
// Bump the counter up to the highest count seen so far
- int backup;
- if (SystemInfo.TryParse(curFileName.Substring(index + 1), out backup))
- {
- if (backup > m_curSizeRollBackups)
- {
- if (0 == m_maxSizeRollBackups)
- {
- // Stay at zero when zero backups are desired
- }
- else if (-1 == m_maxSizeRollBackups)
- {
- // Infinite backups, so go as high as the highest value
- m_curSizeRollBackups = backup;
- }
- else
- {
- // Backups limited to a finite number
- if (m_countDirection >= 0)
- {
- // Go with the highest file when counting up
- m_curSizeRollBackups = backup;
- }
- else
- {
- // Clip to the limit when counting down
- if (backup <= m_maxSizeRollBackups)
- {
- m_curSizeRollBackups = backup;
- }
- }
- }
- LogLog.Debug(declaringType, "File name ["+curFileName+"] moves current count to ["+m_curSizeRollBackups+"]");
- }
- }
+ int backup = GetBackUpIndex(curFileName);
+
+ if (backup > m_curSizeRollBackups)
+ {
+ if (0 == m_maxSizeRollBackups)
+ {
+ // Stay at zero when zero backups are desired
+ }
+ else if (-1 == m_maxSizeRollBackups)
+ {
+ // Infinite backups, so go as high as the highest value
+ m_curSizeRollBackups = backup;
+ }
+ else
+ {
+ // Backups limited to a finite number
+ if (m_countDirection >= 0)
+ {
+ // Go with the highest file when counting up
+ m_curSizeRollBackups = backup;
+ }
+ else
+ {
+ // Clip to the limit when counting down
+ if (backup <= m_maxSizeRollBackups)
+ {
+ m_curSizeRollBackups = backup;
+ }
+ }
+ }
+ LogLog.Debug(declaringType, "File name [" + curFileName + "] moves current count to [" + m_curSizeRollBackups + "]");
+ }
}
catch(FormatException)
{
@@ -906,7 +926,27 @@
}
}
- ///
+ private int GetBackUpIndex(string curFileName)
+ {
+ int backUpIndex = -1;
+ string fileName = curFileName;
+
+ if (m_preserveLogFileNameExtension)
+ {
+ fileName = Path.GetFileNameWithoutExtension(fileName);
+ }
+
+ // need special logic to handle PreserveLogFileNameExtension=true ???
+ int index = fileName.LastIndexOf(".");
+ if (index > 0)
+ {
+ SystemInfo.TryParse(fileName.Substring(index + 1), out backUpIndex);
+ }
+
+ return backUpIndex;
+ }
+
+ ///
/// Takes a list of files and a base file name, and looks for
/// 'incremented' versions of the base file. Bumps the max
/// count up to the highest count seen.
@@ -1034,7 +1074,7 @@
if (m_rollDate && File != null && m_scheduledFilename == null)
{
- 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));
}
ExistingInit();
@@ -1046,6 +1086,25 @@
#region Roll File
+ ///
+ ///
+ ///
+ ///
+ /// .1, .2, .3, etc.
+ ///
+ private string CombinePath(string path1, string path2)
+ {
+ string extension = Path.GetExtension(path1);
+ if (m_preserveLogFileNameExtension && extension.Length > 0)
+ {
+ return Path.Combine(Path.GetDirectoryName(path1), Path.GetFileNameWithoutExtension(path1) + path2 + extension);
+ }
+ else
+ {
+ return path1 + path2;
+ }
+ }
+
///
/// Rollover the file(s) to date/time tagged file(s).
///
@@ -1072,9 +1131,9 @@
//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;
}
@@ -1087,8 +1146,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);
}
@@ -1099,7 +1158,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)
{
@@ -1308,20 +1367,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
{
@@ -1351,13 +1410,13 @@
}
// Delete the archive file
- DeleteFile(archiveFileBaseName + '.' + oldestFileIndex);
+ DeleteFile(CombinePath(archiveFileBaseName, "." + oldestFileIndex));
}
if (m_staticLogFileName)
{
m_curSizeRollBackups++;
- RollFile(baseFileName, baseFileName + '.' + m_curSizeRollBackups);
+ RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
}
@@ -1528,6 +1587,12 @@
///
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
///