Error log:
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.
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.
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.
To avoid filling up of that particular drive. To change the Sql Server ErrorLog files location you may need to chane in configuration manager of 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.
Location:
Default error log are 6.
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.
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.
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 |
3. Limiting the Size of the Sql Server Error Logs file
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.To avoid filling up of that particular drive. To change the Sql Server ErrorLog files location you may need to chane in configuration manager of sql server.
xp_readerrorlog()
• The first parameter specifies which log to return and the default is “0”.
For the second parameter, you specify which log to review. The default is 1 for the SQL Server database engine, but you can specify a 2 for the SQL Server agent.
The 3rd and 4th parameters allow you to specify 2 search strings to search the log with.
The 5th and 6th allow you to specify start and end date
The 7th is for order by like ascending or descending
So to find the deadlock history from previous error log using xp_readerrorlog, execute the following:
EXEC master.dbo.xp_readerrorlog 3, 1, N'', N'deadlock', '20190222', '20190323', N'desc'
sp_readerrorlog():
sp_readerrorlog 3, 1, N'', N'deadlock'