Something broke at 03:10 and you need the error log for that minute. You don't want to scroll through 400 MB of messages in SSMS. This cheat sheet covers how to find the right file, filter it by text and time, keep it small enough to open, and keep enough history to cover a long weekend.
Where the log lives
The current log is ERRORLOG. Archives are ERRORLOG.1 (newest) to ERRORLOG.n in the instance's Log folder. SQL Server Agent writes its own SQLAGENT.OUT and SQLAGENT.1, SQLAGENT.2 and so on.
-- Full path of the current error log
SELECT SERVERPROPERTY('ErrorLogFileName') AS current_errorlog;
-- List the archives with dates and sizes (undocumented, but present in all current versions)
EXEC master.sys.xp_enumerrorlogs; -- SQL Server log
EXEC master.sys.xp_enumerrorlogs 2; -- Agent log
A new file starts every time the service restarts and every time someone runs sp_cycle_errorlog. The default keeps six archives. On a server that restarts twice during patching, that can be less than a week of history.
Read and filter with xp_readerrorlog
sp_readerrorlog accepts four parameters. The extended procedure xp_readerrorlog behind it accepts seven, including a time window and a sort order, so use that one.
| Position | Meaning | Example |
|---|---|---|
| 1 | Log number: 0 = current, 1 = ERRORLOG.1 | 0 |
| 2 | Log type: 1 = SQL Server, 2 = Agent | 1 |
| 3 | First search string | N'Login failed' |
| 4 | Second search string (AND) | N'reason' |
| 5 | Start time | '2026-11-27 03:00' |
| 6 | End time | '2026-11-27 03:30' |
| 7 | Sort order | N'desc' |
-- Everything in a 30-minute window, newest first
EXEC master.sys.xp_readerrorlog 0, 1, NULL, NULL, '2026-11-27 03:00', '2026-11-27 03:30', N'desc';
-- Failed logins in the current log
EXEC master.sys.xp_readerrorlog 0, 1, N'Login failed', NULL, NULL, NULL, N'desc';
-- Slow I/O warnings in the previous log
EXEC master.sys.xp_readerrorlog 1, 1, N'longer than 15 seconds';
-- Agent log, errors only
EXEC master.sys.xp_readerrorlog 0, 2, N'error';
Search strings are Unicode. Pass them as N'...' or the call can return nothing without raising an error.
Search every archive at once
DROP TABLE IF EXISTS #log;
CREATE TABLE #log (LogDate datetime, ProcessInfo nvarchar(64), [Text] nvarchar(max), ArchiveNo int NULL);
DECLARE @archives TABLE (ArchiveNo int, LogDate datetime, LogSize bigint);
INSERT @archives EXEC master.sys.xp_enumerrorlogs;
DECLARE @n int = (SELECT MAX(ArchiveNo) FROM @archives), @i int = 0;
WHILE @i <= @n
BEGIN
INSERT #log (LogDate, ProcessInfo, [Text])
EXEC master.sys.xp_readerrorlog @i, 1, N'error';
UPDATE #log SET ArchiveNo = @i WHERE ArchiveNo IS NULL;
SET @i += 1;
END;
SELECT ArchiveNo, LogDate, ProcessInfo, [Text]
FROM #log
WHERE [Text] NOT LIKE N'%found 0 errors%'
ORDER BY LogDate DESC;
Messages worth searching for
| Search text | What it usually means | Next step |
|---|---|---|
Login failed | Wrong password, missing login, default database gone | Read the state number; see the error 18456 post |
longer than 15 seconds | Storage latency spikes (error 833) | Check disk latency counters and the storage path |
Error: 823, 824, 825 | I/O or page checksum errors | Run DBCC CHECKDB and check the storage logs |
paged out | Windows trimmed SQL Server's working set | Grant Lock Pages in Memory and set max server memory |
Non-yielding or 17883 | Scheduler stuck and a dump was written | Collect the dump, check the build against known issues |
SQL Server is starting | Restart or failover time | Correlate with Windows System log and cluster log |
The transaction log for database | Error 9002, log full | Check log_reuse_wait_desc |
deadlock | Only appears if trace flag 1222 or 1204 is on | Prefer the system_health session instead |
Read it from PowerShell
dbatools reads the log from many servers at once and returns objects you can filter:
# Last 2 hours of errors on three instances
Get-DbaErrorLog -SqlInstance sql01, sql02, sql03 -After (Get-Date).AddHours(-2) |
Where-Object Text -match 'error|failed' |
Select-Object ComputerName, InstanceName, LogDate, Source, Text |
Sort-Object LogDate -Descending
# Search a specific archive
Get-DbaErrorLog -SqlInstance sql01 -LogNumber 1 -Text 'Login failed'
With the Microsoft SqlServer module:
Get-SqlErrorLog -ServerInstance sql01 -Since Yesterday |
Where-Object Text -like '*error*'
When the instance will not start
You can't query a stopped instance. Find the -e startup parameter in the registry and read the file directly. The instance key name varies by version and instance name (MSSQL16.MSSQLSERVER is SQL Server 2022 default instance), so list the keys first.
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' |
Where-Object PSChildName -like 'MSSQL*.*'
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQLServer\Parameters'
# SQLArg1 is usually -e<path to ERRORLOG>
Get-Content 'D:\MSSQL16.MSSQLSERVER\MSSQL\Log\ERRORLOG' -Tail 60
The last 30–60 lines nearly always name the failing file, database or setting.
Cycle the log on a schedule
Cycling closes the current file and starts a new one without a restart. A daily cycle keeps each file small enough to open in SSMS and maps one file to one day.
EXEC master.sys.sp_cycle_errorlog; -- SQL Server log
EXEC msdb.dbo.sp_cycle_agent_errorlog; -- Agent log
Put both in a SQL Agent job that runs at 00:00. Each cycle deletes the oldest archive once you reach the limit, so set the retention before you add the schedule.
Warning
Every cycle and every restart removes the oldest archive. If you cycle daily but keep only six archives, you lose the evidence from last week's incident. Raise the file count first.
Keep more history
Raise the number of archives from 6 to 30 or more (maximum 99). In SSMS: right-click SQL Server Logs > Configure. The T-SQL equivalent writes to the instance-specific registry key:
EXEC master.sys.xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'NumErrorLogs', REG_DWORD, 30;
-- Check the value
EXEC master.sys.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'NumErrorLogs';
SQL Server 2012 and later also read an ErrorLogSizeInKb value from the same key. It makes the log roll over when the file reaches that size. Recent SSMS versions show it in the same Configure dialog. Set it there and check that a new file appears when the limit is reached, because the registry location has varied between builds.
Cut the noise
- Successful backup messages. Each log backup writes a line. With 200 databases on 15-minute log backups, these lines fill the log. Trace flag 3226 suppresses success messages only, and failures are still logged. Add
-T3226as a startup parameter in SQL Server Configuration Manager, or runDBCC TRACEON (3226, -1);until the next restart. - Successful logins. Set Server Properties > Security > Login auditing to Failed logins only unless an audit requirement says otherwise. Use SQL Server Audit for full login auditing.
Tip
After a restart, check the first 100 lines of the new log. They show the build, the trace flags in effect, Lock Pages in Memory, Instant File Initialization and the startup parameters. Comparing them before and after a change is a quick drift check.
Prevent it next time
- Set
NumErrorLogsto at least 30 on every instance, and add it to your build standard. - Cycle the SQL Server and Agent logs daily with an Agent job.
- Enable trace flag 3226 where log backups are frequent.
- Ship
Login failed, error 823–825, 833 and 17883 lines to your monitoring tool, or alert on them with Agent alerts. Don't wait until someone reads the file. - Keep a saved
xp_readerrorlogscript with time-window parameters in the team repo, so nobody has to scroll through the log during an incident.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get