@@ -52,27 +52,27 @@ public LegacyArchiveFileNameHandler(FileTarget fileTarget)
5252 {
5353 }
5454
55- public override int ArchiveBeforeOpenFile ( string newFilePath , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified , int newSequenceNumber )
55+ public override int ArchiveBeforeOpenFile ( string newFileName , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified , int newSequenceNumber )
5656 {
57- var archiveFilePath = _fileTarget . ArchiveFileName ? . Render ( firstLogEvent ) ;
58- if ( StringHelpers . IsNullOrWhiteSpace ( archiveFilePath ) )
57+ var archiveFileName = _fileTarget . ArchiveFileName ? . Render ( firstLogEvent ) ;
58+ if ( StringHelpers . IsNullOrWhiteSpace ( archiveFileName ) )
5959 {
60- return base . ArchiveBeforeOpenFile ( newFilePath , firstLogEvent , previousFileLastModified , newSequenceNumber ) ;
60+ return base . ArchiveBeforeOpenFile ( newFileName , firstLogEvent , previousFileLastModified , newSequenceNumber ) ;
6161 }
6262
63+ var newFilePath = FileTarget . CleanFullFilePath ( newFileName ) ;
64+
6365 bool initialFileOpen = newSequenceNumber == 0 ;
64- if ( ArchiveBeforeOpenFile ( archiveFilePath , newFilePath , firstLogEvent , previousFileLastModified , initialFileOpen ) )
66+ if ( ArchiveBeforeOpenFile ( archiveFileName , newFilePath , firstLogEvent , previousFileLastModified , initialFileOpen ) )
6567 {
6668 FixWindowsFileSystemTunneling ( newFilePath ) ;
6769 }
6870
6971 return 0 ;
7072 }
7173
72- private bool ArchiveBeforeOpenFile ( string archiveFilePath , string newFilePath , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified , bool initialFileOpen )
74+ private bool ArchiveBeforeOpenFile ( string archiveFileName , string newFilePath , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified , bool initialFileOpen )
7375 {
74- archiveFilePath = FileTarget . CleanFullFilePath ( archiveFilePath ) ;
75-
7676 bool oldFilesDeleted = false ;
7777 if ( _fileTarget . MaxArchiveFiles >= 0 || _fileTarget . MaxArchiveDays > 0 || ( initialFileOpen && _fileTarget . DeleteOldFileOnStartup ) )
7878 {
@@ -82,26 +82,28 @@ private bool ArchiveBeforeOpenFile(string archiveFilePath, string newFilePath, L
8282 var excludeFileName = Path . GetFileName ( newFilePath ) ;
8383 if ( wildCardStrictSeqNo )
8484 {
85- string archiveFileWildcard = BuildArchiveFilePath ( archiveFilePath , int . MaxValue , DateTime . MinValue ) . Replace ( int . MaxValue . ToString ( ) , "*" ) ;
85+ string archiveFilePath = BuildArchiveFilePath ( archiveFileName , int . MaxValue , DateTime . MinValue ) ;
86+ string archiveFileWildcard = archiveFilePath . Replace ( int . MaxValue . ToString ( ) , "*" ) ;
8687 string archiveDirectory = Path . GetDirectoryName ( archiveFilePath ) ;
8788 oldFilesDeleted = DeleteOldFilesBeforeArchive ( archiveDirectory , Path . GetFileName ( archiveFileWildcard ) , initialFileOpen , excludeFileName , true ) ;
8889 }
8990 else
9091 {
92+ var archiveFilePath = FileTarget . CleanFullFilePath ( archiveFileName ) ;
9193 oldFilesDeleted = DeleteOldFilesBeforeArchive ( archiveFilePath , initialFileOpen , excludeFileName ) ;
9294 }
9395 }
9496
9597 if ( initialFileOpen && ! _fileTarget . ArchiveOldFileOnStartup )
9698 return oldFilesDeleted ;
9799
98- if ( ! ArchiveOldFileWithRetry ( archiveFilePath , newFilePath , firstLogEvent , previousFileLastModified ) )
100+ if ( ! ArchiveOldFileWithRetry ( archiveFileName , newFilePath , firstLogEvent , previousFileLastModified ) )
99101 return oldFilesDeleted ;
100102
101103 return true ;
102104 }
103105
104- private bool ArchiveOldFileWithRetry ( string archiveFilePath , string newFilePath , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified )
106+ private bool ArchiveOldFileWithRetry ( string archiveFileName , string newFilePath , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified )
105107 {
106108 DateTime ? lastWriteTimeUtc = default ( DateTime ? ) ;
107109 long ? lastFileLength = default ( long ? ) ;
@@ -125,7 +127,7 @@ private bool ArchiveOldFileWithRetry(string archiveFilePath, string newFilePath,
125127
126128 lastWriteTimeUtc = lastWriteTimeUtc ?? newFileInfo . LastWriteTimeUtc ;
127129 lastFileLength = lastFileLength ?? newFileInfo . Length ;
128- if ( ArchiveOldFile ( newFileInfo , firstLogEvent , previousFileLastModified , archiveFilePath ) )
130+ if ( ArchiveOldFile ( archiveFileName , newFileInfo , firstLogEvent , previousFileLastModified ) )
129131 return true ;
130132 }
131133 catch ( IOException ex )
@@ -148,14 +150,14 @@ private bool ArchiveOldFileWithRetry(string archiveFilePath, string newFilePath,
148150 return oldFilesDeleted ;
149151 }
150152
151- private bool ArchiveOldFile ( FileInfo newFileInfo , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified , string archiveFilePath )
153+ private bool ArchiveOldFile ( string archiveFileName , FileInfo newFileInfo , LogEventInfo firstLogEvent , DateTime ? previousFileLastModified )
152154 {
153155 DateTime fileLastWriteTime = Time . TimeSource . Current . FromSystemTime ( newFileInfo . LastWriteTimeUtc ) ;
154156 if ( previousFileLastModified . HasValue && ( previousFileLastModified > fileLastWriteTime || fileLastWriteTime >= firstLogEvent . TimeStamp ) )
155157 fileLastWriteTime = previousFileLastModified . Value ;
156158
157- var archiveNextSequenceNo = ResolveNextArchiveSequenceNo ( archiveFilePath , fileLastWriteTime ) ;
158- string archiveFullPath = BuildArchiveFilePath ( archiveFilePath , archiveNextSequenceNo , fileLastWriteTime ) ;
159+ var archiveNextSequenceNo = ResolveNextArchiveSequenceNo ( archiveFileName , fileLastWriteTime ) ;
160+ string archiveFullPath = BuildArchiveFilePath ( archiveFileName , archiveNextSequenceNo , fileLastWriteTime ) ;
159161
160162 if ( ! File . Exists ( archiveFullPath ) )
161163 {
@@ -181,17 +183,17 @@ private bool ArchiveOldFile(FileInfo newFileInfo, LogEventInfo firstLogEvent, Da
181183 return false ;
182184 }
183185
184- private string BuildArchiveFilePath ( string archiveFilePath , int archiveNextSequenceNo , DateTime fileLastWriteTime )
186+ private string BuildArchiveFilePath ( string archiveFileName , int archiveNextSequenceNo , DateTime fileLastWriteTime )
185187 {
186- var filePath = _fileTarget . BuildFullFilePath ( archiveFilePath , archiveNextSequenceNo , fileLastWriteTime ) ;
188+ var filePath = _fileTarget . BuildFullFilePath ( archiveFileName , archiveNextSequenceNo , fileLastWriteTime ) ;
187189 var fileName = Path . GetFileName ( filePath ) ;
188190 if ( fileName ? . Length > 0 && fileName [ 0 ] == '_' )
189191 {
190192 var directory = Path . GetDirectoryName ( filePath ) ;
191193 if ( ! string . IsNullOrEmpty ( directory ) )
192194 filePath = Path . Combine ( directory , fileName . Substring ( 1 ) ) ;
193195 }
194- return FileTarget . CleanFullFilePath ( filePath ) ;
196+ return filePath ;
195197 }
196198
197199 private void ArchiveFileAppendExisting ( string fileName , string archiveFileName )
@@ -235,32 +237,30 @@ private void ArchiveFileAppendExisting(string fileName, string archiveFileName)
235237 }
236238 }
237239
238- private int ResolveNextArchiveSequenceNo ( string archiveFilePath , DateTime fileLastWriteTime )
240+ private int ResolveNextArchiveSequenceNo ( string archiveFileName , DateTime fileLastWriteTime )
239241 {
242+ // Archive operation triggered, how to resolve the next archive-sequence-number ?
243+ // - Old version was able to "parse" the file-names of the archive-folder and "guess" the next sequence number
244+ var archiveFilePath = BuildArchiveFilePath ( archiveFileName , int . MaxValue , fileLastWriteTime ) ;
240245 var archiveDirectory = Path . GetDirectoryName ( archiveFilePath ) ;
246+ if ( string . IsNullOrEmpty ( archiveDirectory ) )
247+ return 0 ;
248+
241249 var directoryInfo = new DirectoryInfo ( archiveDirectory ) ;
242250 if ( ! directoryInfo . Exists )
243251 {
244- Directory . CreateDirectory ( directoryInfo . FullName ) ;
252+ directoryInfo . Create ( ) ;
245253 }
246254
247- // Archive operation triggered, how to resolve the next archive-sequence-number ?
248- // - Old version was able to "parse" the file-names of the archive-folder and "guess" the next sequence number
249- var archiveWildCard = BuildArchiveFilePath ( archiveFilePath , int . MaxValue , fileLastWriteTime ) . Replace ( int . MaxValue . ToString ( ) , "*" ) ;
250- if ( archiveWildCard . IndexOf ( '*' ) < 0 )
251- return 0 ;
252-
253- var archiveWildCardFileName = Path . GetFileName ( archiveWildCard ) ;
254- int splitIndex = archiveWildCardFileName . IndexOf ( '*' ) ;
255- if ( splitIndex < 0 )
255+ var archiveWildCardFileName = Path . GetFileName ( archiveFilePath ) . Replace ( int . MaxValue . ToString ( ) , "*" ) ;
256+ int fileWildcardStartIndex = archiveWildCardFileName . IndexOf ( '*' ) ;
257+ if ( fileWildcardStartIndex < 0 )
256258 return 0 ;
257259
258- int fileWildcardStartIndex = archiveWildCardFileName . IndexOf ( '*' ) ;
259260 int fileWildcardEndIndex = archiveWildCardFileName . Length - fileWildcardStartIndex ;
260-
261- if ( splitIndex > 0 && ! char . IsLetter ( archiveWildCardFileName [ splitIndex - 1 ] ) && ! char . IsDigit ( archiveWildCardFileName [ splitIndex - 1 ] ) )
261+ if ( fileWildcardStartIndex > 0 && ! char . IsLetter ( archiveWildCardFileName [ fileWildcardStartIndex - 1 ] ) && ! char . IsDigit ( archiveWildCardFileName [ fileWildcardStartIndex - 1 ] ) )
262262 {
263- archiveWildCardFileName = archiveWildCardFileName . Substring ( 0 , splitIndex - 1 ) + archiveWildCardFileName . Substring ( splitIndex ) ;
263+ archiveWildCardFileName = archiveWildCardFileName . Substring ( 0 , fileWildcardStartIndex - 1 ) + archiveWildCardFileName . Substring ( fileWildcardStartIndex ) ;
264264 }
265265
266266 var archiveFiles = directoryInfo . GetFiles ( archiveWildCardFileName ) ;
0 commit comments