简体   繁体   中英

Why am I getting two log files per day using log4net with ASMX web service?

I have a ASMX web service running IIS 10 and have added log4net to the project with the following configuration:

<appender name="AsmxDebugLogFile" type="log4net.Appender.RollingFileAppender">
  <file value="App_Data/ASMX.DEBUG_" type="log4net.Util.PatternString" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value="yyyy-MM-dd'.log'" />
  <maximumFileSize value="5GB" />
  <staticLogFileName value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>
<logger name="AsmxDebugLogFile">
  <level value="DEBUG" />
  <appender-ref ref="AsmxDebugLogFile" />
</logger>

But for some reason, I'm getting two log files per day in the format of yyyy-MM-dd.log.yyyy-MM-dd.log as per the screenshot below, seemingly at random times as well, whereby the log file will switch to the newly created log file, and then will start logging again in the log file at was created at midnight of the day in question. All log files are way below the 5GB max file size as well.

每天两个 log4net 日志文件

Below is an updated list of log files

在此处输入图像描述

It seems that initially, log4net logs to the file yyyy-MM-dd.log, and then after a random amount of time creates a new log file yyyy-MM-dd.log.yyyy-MM-dd.log starts writing to this log file and then goes back and logs to yyyy-MM-dd.log which was created at the start of the day.

These are the IIS recycle settings

IIS 设置

I am wanting 1 log file per day in the format of ASMX.DEBUG_yyyy-MM-dd.log. Where am I going wrong with this configuration?

UPDATE

It seems that because we are recycling the application pool every 60 minutes depending on whether the log file is being written to at this time, depends on whether a new one is created. Any suggestions as to how to overcome this issue. At the minute I've now added the process_id in the log file name and it seems to work in that I get a new log file created every hour, but ideally I want just 1 log file a day.

<appender name="AsmxDebugLogFile" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

I think you need Locking model which will not lock your file.I think when main file gets created and get locked. The File appender creates file name with same date time stamp over the exisiting file. You should also use Remote path for logging like \\server\file\debug since you might have load balancer for IIS or Web server is hosted with static IP or F5.

I use log4net for all my applications and I never encountered this re-creation of file after a pool recycling.

Here are my parameter that differ from yours:

<appendToFile value="true"/>
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

And here's how the behaviour will be different:

  • staticLogFileName means current day's file name will always be the same ( ASMX.DEBUG_.log here). It will be automatically renamed with the date pattern on the day after.
  • appendToFile means it will not overwrite current day's file, but rather append to it.

I think those first two parameters will avoid the creation of multiple file for the current day which may fix your problem.

  • MinimalLock 's role is less clear to me, but I should mention it anyway:

Opens the file once for each AcquireLock()/ReleaseLock() cycle, thus holding the lock for the minimal amount of time. This method of locking is considerably slower than FileAppender. ExclusiveLock but allows other processes to move/delete the log file whilst logging continues.

Try with this parameters.

  <param name="File" value="C:\YourLogFilePath\" />
  <rollingStyle value="Composite" />
  <param name="DatePattern" value="dd.MM.yyyy'.log'" />
  <param name="AppendToFile" value="true" />
  <maxSizeRollBackups value="2" />
  <maximumFileSize value="5GB" />
  <staticLogFileName value="false" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

You are using the "RollingFileAppender" which as its name suggests rolls to a new file on dates, try using the FileAppender, more information about how to use FileAppender you can refer to this link: FileAppender .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM