2

我们有几个使用 log4net 的 ASP.NET MVC 实例。实例很少将信息记录到指定的日志文件中。但是仍然会生成大量的 log4net 文件,其中填充了我认为是实际 log4net 设置的日志记录。

生成的文件格式为 [guid]log4net.txt。示例:6cec7387-2ca2-4186-b40d-35bf1f3fad39log4net.txt

并包含重复到几个 mb 文件的此类信息:

log4net: log4net assembly [log4net, Version=1.2.11.0, Culture=neutral,                                          PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\5ce477cb\c777cea8\assembly\dl3\8f934d8e\ab938bbc_bfb9ce01\log4net.dll]. (.NET Runtime [4.0.30319.18052] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [<application assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [<application assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\5ce477cb\c777cea8\assembly\dl3\7ff992c2\7d13e38d_9ac9ce01\<application assembly>.dll]
log4net: Assembly [<application assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [<application assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: configuring repository [log4net-default-repository] using file [<application path>\Web.config] watching for file updates
log4net: configuring repository [log4net-default-repository] using file [<application path>\Web.config]
log4net: configuring repository [log4net-default-repository] using stream
log4net: loading XML configuration
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [DEBUG].
log4net: Logger [root] level set to [name="DEBUG",value=30000].
log4net: Loading Appender [LogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: Parameter [file] specified subtype [log4net.Util.PatternString]
log4net: Converter [literal] Option [.\\App_Data\\Log[] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [processid] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option []] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternString] to [String]
log4net: Setting Property [File] to String value [.\\App_Data\\Log[7392]]
log4net: Setting Property [AppendToFile] to Boolean value [True]
log4net: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: Setting Property [MaximumFileSize] to String value [10MB]
log4net: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: Setting Property [LockingModel] to object [log4net.Appender.FileAppender+MinimalLock]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n]
log4net: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [d] Option [yyyy-MM-dd hh:mm:ss] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ – ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [n] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Searched for existing files in [<application path>\App_Data]
log4net: curSizeRollBackups starts at [0]
log4net: Opening file for writing [<application path>\App_Data\Log[7392]] append [True]
log4net: reated Appender [LogFileAppender]
log4net: Adding appender named [LogFileAppender] to logger [root].
log4net: Hierarchy Threshold []
log4net: configuring repository [log4net-default-repository] using .config file section
log4net: Application config file is [<application path>\web.config]

web.config 的相关部分如下所示:

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value=".\\App_Data\\Log[%processid]" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
  </log4net>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="<logs path>\log4net.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

日志配置如下:

XmlConfigurator.Configure();

并用作:

    protected static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

通过定期致电警告等。

我们尝试更改 LogFileAppender 的设置以在不同的进程中使用不同的文件(如示例中所示)。并确保生成的文件中没有错误或冲突。日志记录按预期工作到正确的文件,问题是上述生成的文件大约是 1 GB/天,这很快就会填满我们的服务器。=/

我们如何停止 log4net 生成这些文件?(或减少到只生成一个有大小限制的)

如果您有任何问题或需要更多信息,请回复。

4

1 回答 1

1

看起来您已启用 log4net 的内部调试

您可以通过添加一个appSettings元素从 web.config 执行此操作:

<add key="log4net.Internal.Debug" value="true" />

或从代码:

log4net.Util.LogLog.InternalDebugging = true;

如果这发生在你程序的任何地方,log4net 将开始记录它所采取的每一个动作。

于 2013-10-23T10:05:38.210 回答