How to Manage Log Retention in Sitecore 9 (including xConnect)
Sitecore logs are a helpful resource for anyone managing their Sitecore environment… but as part of good Sitecore governance, you need to adjust the logging retention for how long to keep your Sitecore logs (this can be shorter or longer than the defaults).
As the Sitecore CMS, Processing, Reporting, and CD Roles are different than xConnect… there are different methods for adjusting the logging depending if its Sitecore or xConnect.
How Long are Logs Retained by Default?
By default the Sitecore roles of the CMS, Processing, Reporting and CDS retain logs for 30 days. xConnect roles maintain logs for 7 days.
How to Change Sitecore Log Retention
As previously stated, the Sitecore roles of CMS, Processing, Reporting and CDS have a method for log retention that is distinct from xConnect.
Sitecore roles use the Sitecore CleanupAgent Task to trim logs. You can change a host of settings as it pertains to the Sitecore.Tasks.CleanupAgent including how often the agent runs (6 hours by default) and how long to retain log files (and even diagnostics, viewstate, and MediaCache).
The retention setting attribute of maxAge is what you want to adjust. So you could adjust the logs setting down or up depending on your needs so long as you follow the proper date format (ex. default log age of 30 days is defined as maxAge=”30.00:00:00″)
Important: You need to adjust the logging per Sitecore role
The Sitecore.Tasks.CleanupAgent settings are defined in the Sitecore.config of your role (example path: {Sitecore Webroot}\App_Config\Sitecore.config) and should look like the following:
<agent type="Sitecore.Tasks.CleanupAgent" method="Run" interval="06:00:00">
<!-- Specifies files to be cleaned up.
If rolling="true", [minCount] and [maxCount] will be ignored.
[minAge] and [maxAge] must be specified as [days.]hh:mm:ss. The default value
of [minAge] is 30 minutes.
[strategy]: number of files within hour, day, week, month, year
[recursive=true|false]: descend folders?
-->
<files hint="raw:AddCommand">
<remove folder="$(dataFolder)/logs" pattern="*log.*.txt" maxAge="30.00:00:00" />
<remove folder="$(dataFolder)/diagnostics" pattern="*.*" maxAge="30.00:00:00" recursive="true" />
<remove folder="$(dataFolder)/viewstate" pattern="*.txt" maxAge="2.00:00:00" recursive="true" />
<remove folder="$(tempFolder)/diagnostics" pattern="*.*" maxAge="00:10:00" recursive="true" />
<remove folder="/App_Data/MediaCache" pattern="*.*" maxAge="90.00:00:00" recursive="true" />
</files>
</agent>
How to Change xConnect Log Retention
xConnect logging is managed in the sc.Serilogxml file. This file is located at the following path per xConnect role ({xConnect Webroot}\App_data\config\sitecore\CoreServices\sc.Serilog.xml).
This XML file is not as complex as the Sitecore.config file. If you want to adjust logging you can set the <MinimumLevel> as well as the <retainedFileCountLimit> for the desired number of days retained.
<Settings>
<Serilog>
<Using>
<FileSinkAssembly>Serilog.Sinks.File</FileSinkAssembly>
<RollingFileSinkAssembly>Serilog.Sinks.RollingFile</RollingFileSinkAssembly>
</Using>
<MinimumLevel>
<Default>Information</Default>
</MinimumLevel>
<WriteTo>
<FileSink>
<Name>RollingFile</Name>
<Args>
<pathFormat>App_Data\\Logs\\xconnect-log-${MachineName}-${InstanceName}-{Date}.txt</pathFormat>
<retainedFileCountLimit>7</retainedFileCountLimit>
<buffered>False</buffered>
</Args>
</FileSink>
</WriteTo>
<Properties>
<Application>XConnect</Application>
</Properties>
</Serilog>
</Settings>