我们有几个使用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生成这些文件? (或将其减少为仅生成具有大小限制的一个)
如果您有任何疑问或需要更多信息,请回复。
答案 0 :(得分:1)
您似乎已启用了log4net&#39; internal debugging。
您可以通过添加appSettings
元素从web.config执行此操作:
<add key="log4net.Internal.Debug" value="true" />
或者来自代码:
log4net.Util.LogLog.InternalDebugging = true;
如果程序中的任何地方发生这种情况,log4net将开始记录它所执行的每个操作。