Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileTarget crashes if archive file pattern is the same as the log files #1436

Closed
asherber opened this issue May 4, 2016 · 8 comments
Closed
Labels
Milestone

Comments

@asherber
Copy link

asherber commented May 4, 2016

Hi! Thanks for reporting this feature/bug/question!

Please keep / fill in the relevant info from this template so that we can help you as best as possible.

Type (choose one):

  • Bug

NLog version: latest master from GitHub

Platform: .Net 4.5

Current NLog config (xml or C#, if relevant)

var fileTarget = new FileTarget()
            {
                FileName = "${var:baseFileName}-${date:format=yyyyMMdd}.log",
                Layout = "${message}",
                //ArchiveFileName = "${var:baseFileName}.{#}.log",
                ArchiveFileName = "${var:baseFileName}-{#}.log",
                ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
                ArchiveEvery = FileArchivePeriod.Day,
                ArchiveDateFormat = "yyyyMMdd",
                MaxArchiveFiles = 7,
                ArchiveAboveSize = 1000,
            };

If the character before the replacement pattern {#} is a dash, NLog starts throwing exceptions at the point where the log should roll over.

Exception info from InternalLogger:

2016-05-04 22:17:15.9871 Error Error has been raised. Exception: System.ArgumentOutOfRangeException: Length cannot be less than zero.                                                                                                                                 
Parameter name: length                                                                                                             
   at System.String.Substring(Int32 startIndex, Int32 length)                                                                      
   at NLog.Targets.FileTarget.TryParseDateAndSequence(String archiveFileNameWithoutPath, String dateFormat, FileNameTemplate fileTe
mplate, DateTime& date, Int32& sequence) in P:\Visual Studio\NLog\src\NLog\Targets\FileTarget.cs:line 1416                         
   at NLog.Targets.FileTarget.<FindDateAndSequenceArchives>d__158.MoveNext() in P:\Visual Studio\NLog\src\NLog\Targets\FileTarget.c
s:line 1382                                                                                                                        
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)                                                            
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)                                                                 
   at NLog.Targets.FileTarget.ArchiveByDateAndSequence(String fileName, String pattern, LogEventInfo logEvent) in P:\Visual Studio\
NLog\src\NLog\Targets\FileTarget.cs:line 1303                                                                                      
   at NLog.Targets.FileTarget.DoAutoArchive(String fileName, LogEventInfo eventInfo) in P:\Visual Studio\NLog\src\NLog\Targets\File
Target.cs:line 1664                                                                                                                
   at NLog.Targets.FileTarget.ProcessLogEvent(LogEventInfo logEvent, String fileName, Byte[] bytesToWrite) in P:\Visual Studio\NLog
\src\NLog\Targets\FileTarget.cs:line 972                                                                                           
   at NLog.Targets.FileTarget.Write(LogEventInfo logEvent) in P:\Visual Studio\NLog\src\NLog\Targets\FileTarget.cs:line 909        
   at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent) in P:\Visual Studio\NLog\src\NLog\Targets\Target.cs:line 467           
@asherber
Copy link
Author

asherber commented May 5, 2016

Slight correction: It's something about the combination of the punctuation in the FileName and ArchiveFileName that is the problem.

In the example config above, FileName and ArchiveFileName both have a dash before the last part of the filename, and the exception gets thrown. The same is true if I replace the dash in both properties with a period. But if either one is a period and the other one is a dash, then there is no exception and files get written as expected.

@304NotModified
Copy link
Member

This is indeed a bug. Nlog tries to parse the date from current archived files, but failed.

@304NotModified 304NotModified added the bug Bug report / Bug fix label May 5, 2016
@304NotModified 304NotModified added this to the 4.3.4 milestone May 5, 2016
@304NotModified 304NotModified modified the milestones: 4.3.5, 4.3.4 May 16, 2016
@304NotModified
Copy link
Member

304NotModified commented May 16, 2016

Too had no time to look at this. If you could help writing an unit test, that would boost the development!

@asherber
Copy link
Author

Sure, I"ll try.

@304NotModified
Copy link
Member

Thanks! We use xunit1 and you can send a PR :)

@304NotModified 304NotModified modified the milestones: 4.3.5, 4.3.6 Jun 12, 2016
@304NotModified 304NotModified modified the milestones: 4.3.6, 4.3.7 Jul 23, 2016
@304NotModified 304NotModified modified the milestones: 4.3.7, 4.3.8 Aug 5, 2016
@304NotModified 304NotModified modified the milestones: 4.4, 4.3.8 Sep 3, 2016
@304NotModified
Copy link
Member

working in this now

@304NotModified
Copy link
Member

304NotModified commented Sep 10, 2016

I think the problem is here that the archive filename is the same as the filename (only change is the sequence number). They are both in the same dir also.

I couldn't get the same exception, but I think I have solved the issue.

@304NotModified 304NotModified modified the milestones: 4.3.9, 4.4 Sep 10, 2016
@asherber
Copy link
Author

Thank you for picking this up.

@304NotModified 304NotModified changed the title FileTarget archive by DateAndSequence can't have a dash before replacement pattern FileTarget crashes if archive file pattern is the same as the log files Sep 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants