SQL Server 使用ErrorLog记录SQL Server启动和运行过程中的信息,具体信息参考:《SQLSERVER errorlog讲解》。通常来说,ErrorLog是指SQL Server Error Log,其实,SQL Server存在另外一种类型,SQL Server Agent ErrorLog,用于记录Agent的运行信息。

默认情况下,SQL Server 保存 7 个 ErrorLog 文件,分别命名为: ErrorLog,ErrorLog.n(n=1,2,3,4,5,6)。ErrorLog 文件包含的信息最新,ErrorLog.6 文件包含的信息最老。每次重启 SQL Server 时,这些日志文件都做如下循环: 删除 ErrorLog.6 文件,将ErrorLog.5重命名为ErrorLog.6,依次类推,直到将ErrorLog重命名为ErrorLog.1,最后新建一个ErrorLog文件,用于存储SQL Server 运行过程的信息。

SQL Server Agent ErrorLog的文件名是:SQLAgent.n(n=1,2,3,4,5,,,,)。

ErrorLog的存放路径是在:C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log

1,使用SSMS查看

2,使用TSQL 脚本查看

2.1 查看Errorlog的信息

使用 sys.xp_enumerrorlogs 查看 ErrorLog Files的创建日期和大小,其创建日期就是第一条记录插入的日期。

exec sys.xp_enumerrorlogs

切换ErrorLog,查看ErrorLog Files的信息

dbcc errorlog

2.2,读取ErrorLog的信息

SQL Server提供了存储过程sys.xp_readerrorlog及sys.sp_readerrorlog,用于查看Errorlog记录的错误日志。

sys.xp_readerrorlog 有7个参数

1. 存档编号(0~99),其值是 sys.xp_enumerrorlogs返回的Archive#字段的值, 0 代表的是ErrorLog,1代表的是ErrorLog.1。

2. 日志类型(1为SQL Server日志,2为SQL Server Agent日志)

3. 查询包含的字符串

4. 查询包含的字符串,参数4和参数5的逻辑关系是and(与关系)

5. LogDate开始时间

6. LogDate结束时间

7. 结果排序,按LogDate排序(Desc、Asc)

sys.sp_readerrorlog有四个参数,和sys.xp_readerrorlog的前四个参数相同,sys.sp_readerrorlog内部使用sys.xp_readerrorlog来实现。

查看登陆失败的错误日志,可以看到参数4和参数5是过滤Text字段。

exec sys.sp_readerrorlog 0,1,'login','failed'

3,ErrorLog的Rollover

如果Current ErrorLog 文件很大,那么加载和查看的过程十分缓慢,可以运行 sys.sp_cycle_errorlog 或 DBCC ErrorLog,或exec ('DBCC ErrorLog'),手动强制ErrorLog 文件迭代,避免单个ErrorLog File Size过大。每次启动 SQL Server 时,ErrorLog会自动迭代。由于ErrorLog的Rollover会删除时间最老的ErrorLog.6 文件,如果需要保存ErrorLog,在执行 sys.sp_cycle_errorlog 或 DBCC ErrorLog 之前,需要将ErrorLog.6 文件复制到其他存储介质上。

推荐阅读:

SQL Server 错误日志过滤(ERRORLOG)

SQL Server 错误日志收缩(ERRORLOG)

Appendix

Talking about SQL Server and SQL Server agent error logs. Here are few interesting things that we can do from query analyzer in order to read and analyze the SQL Server and agent error logs.

1, sys.XP_READERRORLOG

Syntax: xp_ReadErrorLog a,b,c,d

a -> default is 0. It accepts only integers. 0 mean the current error log, 1 means 1st archive and so on.

b -> default is 1. Accepts value equals 1 for SQL Server error log and 2 for SQL Agent error log.

c -> accepts varchar chanracter upto 255 characters. Default is Null.

d -> accepts varchar chanracter upto 255 characters. Default is Null.

1) To read the current SQL Server error log i.e. ERRORLOG file

xp_readerrorlog 
xp_readerrorlog 0 xp_readerrorlog 0,1

2) TO read SQL Server error log archive 1 i.e. ERRORLOG.1 file

xp_readerrorlog 1 xp_readerrorlog 1,1

3) To read the current SQL Server Agent error log i.e. SQLAGENT.OUT file

xp_readerrorlog 0,2

4) To read SQL Server error log archive 1 i.e. SQLAGENT.1 file

xp_readerrorlog 1,2

5) To search for any specific text in current SQL Server error log

xp_ReadErrorLog 0, 1, 'Failed'

6) To search for entries in current SQL Server error log which contain both ‘Failed’ and ‘Login’ in a row.

xp_ReadErrorLog 0, 1, 'Failed'.'Login'

2,sys.XP_ENUMERRORLOGS

Syntax: xp_enumerrorlogs a

a -> default is 1. Accepts value equals 1 for SQL Server error log and 2 for SQL Agent error log.

1) List all the avilable SQL Server error log archives, there last modified date and size.

xp_enumerrorlogs 
xp_enumerrorlogs 1

2) List all the avilable SQL Server Agent error log archives, there last modified date and size.

xp_enumerrorlogs 2

3,recycle errorlog

To Recycle Current SQL Server Error Log

exec sp_cycle_errorlog

 To Recycle Current SQL Server Agent Error Log

exec sp_cycle_agent_errorlog