【问题标题】:How to compress log files with NLog如何使用 NLog 压缩日志文件
【发布时间】:2019-11-01 00:33:30
【问题描述】:

我在我的一个项目中使用 NLog,我正在尝试压缩文件的输出。 我尝试使用压缩文件属性,但是当我查看文件时,它们没有被压缩。

你能告诉我我做错了什么吗?

这是我的配置:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>  
        <target name="file" xsi:type="File" fileName="C:\Workspaces\log.xml"
                layout="${message}" keepFileOpen="true"
                archiveFileName = "C:\Workspaces\archived\log.{#####}.xml"
                archiveAboveSize = "1048576" archiveNumbering = "Sequence"
                fileAttributes="Compressed" concurrentWrites =  "true"/>
    </targets>

    <rules>
        <logger name ="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog> 

【问题讨论】:

  • 驱动器上是否启用了文件压缩?创建一个包含一些内容的普通文件,制作一个副本,然后将此副本标记为压缩 - 比较两者的文件大小。
  • 这似乎是通过操作系统的解决方案,但不是我正在寻找的解决方案。给出的示例:如果我想将该文件移动到另一个系统,它仍然会被压缩吗?
  • 这似乎是一个报告的错误,you could comment in git-hub 所以他们可以更优先考虑这个问题

标签: compression nlog


【解决方案1】:

使用 NLog 4,您可以压缩到 zip 文件 (.NET 4.5+)。见the NLog 4.0 release post

如下使用enableArchiveFileCompression

<target name="file" xsi:type="File"
      layout="${longdate} ${logger} ${message}" 
      fileName="${basedir}/logs/logfile.txt" 
      archiveFileName="${basedir}/archives/log.{#}.txt"
      archiveEvery="Day"
      archiveNumbering="Rolling"
      maxArchiveFiles="7"
    enableArchiveFileCompression="true" />

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2013-12-04
    • 2013-12-08
    • 2015-08-15
    • 2023-03-13
    • 2016-02-08
    • 2015-06-04
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多