Reputation: 551
I have a WCF service and want to enable logging. As described in Microsoft's documentation Configuring Message Logging, I put in the Web.config the following:
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\logs\messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>
How can I rotate the log, for example write to messages1.svclog, then messages2.svclog, etc. when the number of messages in the last log has reached maxMessagesToLog=3000?
I read the article A Rolling XmlWriterTraceListener, but this rotates the log file when a certain file size has been reached. However, when the number of messages reaches maxMessagesToLog, WCF will stop logging.
Upvotes: 1
Views: 1280
Reputation: 1
Another option is to use symbolic links. You can delete and reset symbolic link while IIS is running. Then when it recycles or restarts a new file will be created.
for example you can create a symbolic link with a command like this:
mklink server_tracelog.svclog server_tracelog_1.svclog
You then set in the web.config this file 'server_tracelog.svclog', but this will be the real file it writes to 'server_tracelog_1.svclog'
Then maybe using a schedule task you could run a batch file that has something like this:
REM set day of week as %DAYOFWEEK%
for /f %%a in ('wmic path win32_localtime get DAYOFWEEK /format:list ^| findstr "="') do (set %%a)
del server_tracelog.svclog
del server_tracelog_dayofweek_%DAYOFWEEK%.svclog
mklink server_tracelog.svclog server_tracelog_dayofweek_%DAYOFWEEK%.svclog
Then if your app pool recycles once a day, you will have a new log created each day.
Upvotes: 0
Reputation: 551
I was not able to find out how to rotate the log file before maxMessagesToLog is reached.
I decided to rotate the log file every midnight. Setting maxMessagesToLog to max integer = 2147483647, the number of messages in one day will never exceed that value. I used the custom trace listener as described in the WCF Forum.
public class CustomTraceListener : XmlWriterTraceListener
{
public CustomTraceListener(string fileName)
: base(string.Format(fileName,
string.Format("{0}{1}{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year)))
{
}
}
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="traceListener"
type="MyService.CustomTraceListener, MyService"
initializeData= "c:\wcflogs\messages_{0}.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
I created my application pool with Recycling Specific Times = 00:00:00
appcmd add apppool /name:MyApppool/enable32BitAppOnWin64:true /+recycling.periodicRestart.schedule.[value='00:00:00']
The advanced settings of the application pool look with this command as follows:
Therefore, the process is recycled every midnight, and the messages counter starts from 0. As long as I have less then 2 billion messages per day, I will have all messages in the log file.
Upvotes: 1