Tag Archives: Re-Initializing the Error Log file

Error Logs Maintenance in Sql Server

Sql Server Error Log files contain informational messages, warnings, critical server events, auditing information etc. And this information in the log file is very critical for analyzing any Sql Server issues.

If proper steps towards the maintenance of the Sql Server Error Logs is not take then the error log file size may grow very large making it time consuming and difficult to analyze the issues. And also if proper maintenance is not taken care we lose the history of the error logs. This article explains the various maintenance strategies for the Error Logs.

In Sql Server by default there will be one current error log file i.e. ERRORLOG and six archived log files ERRORLOG.1 (i.e. Archive #1), ERRORLOG.2 (i.e. Archive #2), …. & ERRORLOG.6 (Archive #6). Each time the Sql Server restarts, the last Archive #6 (i.e. ERRORLOG.6) is deleted, Archive #5 is moved as Archive #6 (i.e. ERRORLOG.5 -> ERRORLOG.6), Archive #4 is moved as Archive #5 (i.e. ERRORLOG.4 -> ERRORLOG.5) and So on. Current Error Log (i.e. ERRORLOG) is moved as Archive #1 (i.e. ERRORLOG -> ERRORLOG.1). And a new current error log file (i.e. ERRORLOG) is created.

1. Recycling or Re-Initializing the Error Log file.

In Sql Server by default the Error Log files are recycled only when the Sql Server Service is restarted. Otherwise, it will keep appending the errors, warnings, information etc to the same current error log file i.e. ERRORLOG. If Sql Server service restart is a rare event then the current log file will be of very huge size.

Sql Server provides a mechanism where we don’t need to restart the service to recycle the error logs, instead we can execute the system stored procedure SP_CYCLE_ERRORLOG to reinitialize the error logs.

EXECUTE SP_CYCLE_ERRORLOG 

Below image shows the Sql Server Error Logs folder view with the Error log files before and after recycling the error logs by executing the stored procedure SP_CYCLE_ERRORLOG

recycle-error-log-file-comparision

Below image shows the SSMS Sql Server Logs view before and after recycling the error logs by executing the stored procedure SP_CYCLE_ERRORLOG

recycle-error-log

So, it is a good strategy to create a Schedule Job which executes daily or at some frequency to recycle the error logs by executing the system stored procedure SP_CYCLE_ERRORLOG.

2. Increasing the number of Sql Server Error Logs files

By default as explained above Sql Server will have one Current Sql Server Log file and 6 Archived error log files. If we are recycling the error logs daily, then at any given point in time we will have the history of the error logs of the last 6 days excluding the current day.

Sql Server provides a mechanism where we can increase the number of archived error log files from the default 6 to any number between 6 to 99.

Follow the following steps to change the number of archived error log files.

Step 1: In the SSMS object explorer expand the Management folder and then right click on the Sql Server Logs folder and from the context menu click on the Configure option.

sql-server-error-logs-configuration-change

Step 2: In the Sql Server Error Logs configuration window as shown in the below image change the Maximum number of error log files from the default 6 to the desired number of error log files that you want to maintain.

change-maximum-number-of-error-log-files

You can as-well change the number of Sql Server Error Logs files by executing the following statement. I have tested it in Sql Server 2012 and it worked for me.

EXECUTE SYS.XP_INSTANCE_REGWRITE
	N'HKEY_LOCAL_MACHINE' ,
	N'Software\Microsoft\MSSQLServer\MSSQLServer' ,
	N'NumErrorLogs' ,
	REG_DWORD ,
	99

3. Limiting the Size of the Sql Server Error Logs file

In Sql Server 2012 on wards we can set the maximum size of the each Sql Server Error Log file. For example we can execute the following script in Sql Server 2012, to set the maximum size of each error log file as 10 MB. Setting this value to 10 MB makes sure that the error logs are recycled each time the current log file reaches the 10 MB size.

EXEC SYS.XP_INSTANCE_REGWRITE 
	N'HKEY_LOCAL_MACHINE',
	N'Software\Microsoft\MSSQLServer\MSSQLServer',
	N'ErrorLogSizeInKb', 
	REG_DWORD, 
	10240

4. Changing the Sql Server Error Logs folder location

By default Sql Server ErrorLog files are located in the same drive/path which is used/selected during Sql Server Installation. You may like to store Sql Server Error log files in a drive or path which is other than the one used for Sql Server installation, to avoid filling up of that particular drive. To change the Sql Server ErrorLog files location you may like to read the article: How to change Sql Server ErrorLog files location