Hide
when set the <PreserveLogFileNameExtension value="true" /> ,the <MaxSizeRollBackups value="2"/> can not works.
The issus is caused by func. RollingFileAppender:RollOverRenameFiles(string baseFileName)
and I changed to this.
protected void RollOverSize()
{
this.CloseFile(); // keep windows happy.
LogLog.Debug(declaringType, "rolling over count ["+((CountingQuietTextWriter)QuietWriter).Count+"]");
LogLog.Debug(declaringType, "maxSizeRollBackups ["+m_maxSizeRollBackups+"]");
LogLog.Debug(declaringType, "curSizeRollBackups ["+m_curSizeRollBackups+"]");
LogLog.Debug(declaringType, "countDirection ["+m_countDirection+"]");
RollOverRenameFiles(File);
//if (!m_staticLogFileName && m_countDirection >= 0)
//{
// m_curSizeRollBackups++;
//}
// This will also close the file. This is OK since multiple close operations are safe.
SafeOpenFile(m_baseFileName, false);
}
protected void RollOverRenameFiles(string baseFileName)
{
// If maxBackups <= 0, then there is no file renaming to be done.
if (m_maxSizeRollBackups != 0)
{
if (m_countDirection < 0)
{
// Delete the oldest file, to keep Windows happy.
if (m_curSizeRollBackups == 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((CombinePath(baseFileName, "." + i)), (CombinePath(baseFileName, "." + (i + 1))));
}
m_curSizeRollBackups++;
// Rename fileName to fileName.1
RollFile(baseFileName, CombinePath(baseFileName, ".1"));
}
else
{
//countDirection >= 0
if (m_curSizeRollBackups >= m_maxSizeRollBackups && m_maxSizeRollBackups > 0)
{
if (!m_preserveLogFileNameExtension)
{
//delete the first and keep counting up.
int oldestFileIndex = m_curSizeRollBackups - m_maxSizeRollBackups;
// If static then there is 1 file without a number, therefore 1 less archive
oldestFileIndex++;
// If using a static log file then the base for the numbered sequence is the baseFileName passed in
// If not using a static log file then the baseFileName will already have a numbered postfix which
// we must remove, however it may have a date postfix which we must keep!
string archiveFileBaseName = baseFileName;
if (!m_staticLogFileName)
{
int lastDotIndex = archiveFileBaseName.LastIndexOf(".");
if (lastDotIndex >= 0)
{
archiveFileBaseName = archiveFileBaseName.Substring(0, lastDotIndex);
}
}
// Delete the archive file
DeleteFile(CombinePath(archiveFileBaseName, "." + oldestFileIndex));
if (m_staticLogFileName)
{
m_curSizeRollBackups++;
RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
else
{
m_curSizeRollBackups = 0;
}
}
else
{
m_curSizeRollBackups++;
if (m_staticLogFileName)
{
RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
}
}
}
Show
when set the <PreserveLogFileNameExtension value="true" /> ,the <MaxSizeRollBackups value="2"/> can not works.
The issus is caused by func. RollingFileAppender:RollOverRenameFiles(string baseFileName)
and I changed to this.
protected void RollOverSize()
{
this.CloseFile(); // keep windows happy.
LogLog.Debug(declaringType, "rolling over count ["+((CountingQuietTextWriter)QuietWriter).Count+"]");
LogLog.Debug(declaringType, "maxSizeRollBackups ["+m_maxSizeRollBackups+"]");
LogLog.Debug(declaringType, "curSizeRollBackups ["+m_curSizeRollBackups+"]");
LogLog.Debug(declaringType, "countDirection ["+m_countDirection+"]");
RollOverRenameFiles(File);
//if (!m_staticLogFileName && m_countDirection >= 0)
//{
// m_curSizeRollBackups++;
//}
// This will also close the file. This is OK since multiple close operations are safe.
SafeOpenFile(m_baseFileName, false);
}
protected void RollOverRenameFiles(string baseFileName)
{
// If maxBackups <= 0, then there is no file renaming to be done.
if (m_maxSizeRollBackups != 0)
{
if (m_countDirection < 0)
{
// Delete the oldest file, to keep Windows happy.
if (m_curSizeRollBackups == 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((CombinePath(baseFileName, "." + i)), (CombinePath(baseFileName, "." + (i + 1))));
}
m_curSizeRollBackups++;
// Rename fileName to fileName.1
RollFile(baseFileName, CombinePath(baseFileName, ".1"));
}
else
{
//countDirection >= 0
if (m_curSizeRollBackups >= m_maxSizeRollBackups && m_maxSizeRollBackups > 0)
{
if (!m_preserveLogFileNameExtension)
{
//delete the first and keep counting up.
int oldestFileIndex = m_curSizeRollBackups - m_maxSizeRollBackups;
// If static then there is 1 file without a number, therefore 1 less archive
oldestFileIndex++;
// If using a static log file then the base for the numbered sequence is the baseFileName passed in
// If not using a static log file then the baseFileName will already have a numbered postfix which
// we must remove, however it may have a date postfix which we must keep!
string archiveFileBaseName = baseFileName;
if (!m_staticLogFileName)
{
int lastDotIndex = archiveFileBaseName.LastIndexOf(".");
if (lastDotIndex >= 0)
{
archiveFileBaseName = archiveFileBaseName.Substring(0, lastDotIndex);
}
}
// Delete the archive file
DeleteFile(CombinePath(archiveFileBaseName, "." + oldestFileIndex));
if (m_staticLogFileName)
{
m_curSizeRollBackups++;
RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
else
{
m_curSizeRollBackups = 0;
}
}
else
{
m_curSizeRollBackups++;
if (m_staticLogFileName)
{
RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
}
}
}