Visitar URL original
ArchiveAboveSize not working with GZipFile targets · Issue #5910 · NLog/NLog · GitHub
Skip to content

ArchiveAboveSize not working with GZipFile targets #5910

Closed

Description

@Maxyx

NLog version: 6.0.1

Platform: .NET Framework 4.8

Current NLog config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="c:\temp\nlog-internal.log"
      internalLogLevel="Debug">

	<extensions>
		<add assembly="NLog.Targets.GZipFile"/>
	</extensions>
	
    <targets async="true">
        <target xsi:type="GZipFile"
                name="generalLogFile"
                layout="${longdate}|${level:uppercase=true}|${threadid}|${logger}|${message}|${replace-newlines:${exception:format=ToString,Data}}"
                fileName="${specialfolder:folder=CommonApplicationData:cached=true}\Scanacon\SA80\Logs\SA80_Logs.gz
                archiveFileName="${specialfolder:folder=CommonApplicationData:cached=true}\Scanacon\SA80\Logs\Archive\SA80_Logs.gz"
                archiveSuffixFormat="_{1:yyyyMMdd}_{0:000}"
                archiveEvery="Day"
                archiveAboveSize="104800"
                maxArchiveDays="30"
		compressionLevel="Optimal"/>
        <target xsi:type="GZipFile"
                name="dcuLogFile"
                layout="${longdate}|${level:uppercase=true}|${threadid}|${logger}|${message}|${replace-newlines:${exception:format=ToString,Data}}"
		archiveFileName="${specialfolder:folder=CommonApplicationData:cached=true}\Scanacon\SA80\Logs\Archive\SA80_DCU_Logs.gz"
                fileName="${specialfolder:folder=CommonApplicationData:cached=true}\Scanacon\SA80\Logs\SA80_DCU_Logs.gz"
                archiveSuffixFormat="_{1:yyyyMMdd}_{0:000}"
                archiveEvery="Day"
                archiveAboveSize="104800"
                maxArchiveDays="30"
		compressionLevel="Optimal"/>

    </targets>

    <rules>
        <logger name="*DCU*" minlevel="Warn" writeTo="generalLogFile,dcuLogFile" ruleName="dcuGeneralLogFileRule" final="true"/>
	<logger name="*DCU*" minlevel="Trace" writeTo="dcuLogFile" ruleName="dcuLogFileRule" final="true"/>
        <logger name="*" minlevel="Trace" writeTo="generalLogFile" ruleName="generalLogFileRule"/>
        <logger name="*" minlevel="Trace" writeTo="dcuLogFile"/>
    </rules>

</nlog>  
  • What is the current result?

Logging doesn't work and thousands of empty .gz files are created.

Image

nlog-internal.log

  • Please post full exception details (message, stacktrace, inner exceptions)
2025-07-01 14:51:08.4578 Error GZipFileTarget(Name=generalLogFile_wrapped): Failed writing to FileName: 'C:\ProgramData\Scanacon\SA80\Logs\SA80_Logs.gz' Exception: System.NotSupportedException: This operation is not supported.
   at System.IO.Compression.GZipStream.get_Length()
   at NLog.Targets.FileAppenders.ExclusiveFileLockingAppender.get_FileSize()
   at NLog.Targets.FileTarget.MustArchiveBySize(IFileAppender fileAppender)
   at NLog.Targets.FileTarget.MustArchiveFile(IFileAppender fileAppender, LogEventInfo firstLogEvent)
   at NLog.Targets.FileTarget.RollArchiveFile(String filename, OpenFileAppender openFile, LogEventInfo firstLogEvent, Boolean hasWritten)
   at NLog.Targets.FileTarget.WriteBytesToFile(String filename, LogEventInfo firstLogEvent, MemoryStream ms)
   at NLog.Targets.FileTarget.WriteLogEventsToFile(String filename, IList`1 logEvents)
   at NLog.Targets.FileTarget.Write(IList`1 logEvents)

  • Are there any workarounds?
    No

The reason I want to archive file above a specific size (say 100Mb, NLog.config above has small size for testing) is because my application is log intensive and text files bigger than say 100Mb are slow/impossible to open with some applications thus making them pretty much useless (unless chopped in multiple files but that's an extra step to perform).

I also have to work with limited storage spaces on my machines which is why I want to compress the archives.

Activity

  1. snakefoot commented on Jul 1, 2025

    @snakefoot
    Contributor

    Thank you for the feedback, and happy that NLog.Targets.GZipFile is useful.

    I don't see an easy way of retrieving the actual filesize of a compressed GZip-file (without performance overhead). But NLog supports "counted" filesize where NLog keeps track of how much data (uncompressed) have been written to a single file. This allows NLog to roll when ex. 100 MByte have been written to the GZip-file (ex. compressed to 50 MByte actual filesize).

    I have created #5911 that allows NLog.Targets.GZipFile to also rely on "counted" file-size. Preview-build is available here:

  2. Maxyx commented on Jul 3, 2025

    @Maxyx
    Author

    Thank you for the quick support !
    Sorry for taking some time to reply, took me time to get back in a state where I could test this.

    So far the archiving based on file size is working.
    I still need to test if the archiving based on day (archiveEvery="Day") is working. I'll come back to you tomorrow.

    Thanks again for such a quick support.

  3. snakefoot commented on Jul 3, 2025

    @snakefoot
    Contributor

    Happy to hear that archiveAboveSize is now working with GZipFile. Believe that archiveEvery="Day" should work just fine, since the file-timestamps are not extracted from the GZipStream.

    It will probably take a few weeks before the next NLog-release, but any early feedback is most welcome.

  4. snakefoot commented on Jul 9, 2025

    @snakefoot
    Contributor

    Closing as resolved with #5911, which will be released with NLog v6.0.2

  5. added this to the 6.0.2 milestone on Jul 9, 2025
  6. snakefoot commented on Jul 20, 2025

    @snakefoot
    Contributor

    NLog v6.0.2 has been released:

    Thank you again for the feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions