Find out date and or number of error logs xp_readerrorlog I have some code to help automate checking the error logs daily on our SQL Server.  This code calls xp_readerrorlog 0 to check the error log, and xp_readerrorlog 0,1 to check the SQL Server log.

Sometimes, however, there is more than one error log I need to scan.  

I would like to be able to see, by T-SQL, the date of the log or, barring that, find the number of logs that exist.

I read that we can have up to 99 archive logs, but the server throws the following error message when trying to display an error log that doesn't exist.

"xp_readerrorlog() returned error 2, 'The system cannot find the file specified."

I know the date information and number exists since SSMS displays it( see the image below.)
If you run EXEC sys.sp_enumerrorlogs; it will return one row for each log file, the oldest date in the log file, and the log file size.

Which means you can do something like this:

CREATE TABLE #error_log
(
    log_number INT,
    log_date DATE,
    log_size INT
);
INSERT #error_log ( log_number, log_date, log_size )
EXEC ( 'EXEC sys.sp_enumerrorlogs;' );

SELECT *
FROM   #error_log AS el;


Then set up a loop to grab the max log number, only grab logs after a certain date, or perhaps exclude logs based on size. Some small logs may be uninteresting, and some large logs may take too long to process.