Index: RollingFileAppender.cs =================================================================== --- RollingFileAppender.cs (revision 703602) +++ 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. @@ -1046,6 +1066,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). /// @@ -1072,9 +1105,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 +1120,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 +1132,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 +1341,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 +1384,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 +1561,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 ///