This article explores the xp_readerrorlog command for reading SQL Server error logs using T-SQL.

本文探讨了xp_readerrorlog命令,该命令用于使用T-SQL读取SQL Server错误日志。

介绍 (Introduction)

SQL Server error logs are useful in troubleshooting issues with the database server. It contains both system and user-defined events information. We can use error logs in the following scenario:

SQL Server错误日志对于解决数据库服务器的问题很有用。 它包含系统事件和用户定义的事件信息。 在以下情况下,我们可以使用错误日志:

  • Information about SQL Server instance 有关SQL Server实例的信息
  • Audit login – Success or failure 审核登录–成功或失败
  • Database recovery status 数据库恢复状态
  • Logged errors messages 记录的错误消息

We can read the error logs using different methods, but firstly, we should know the path of the error logs.

我们可以使用不同的方法读取错误日志,但是首先,我们应该知道错误日志的路径。

SSMS GUI方法 (SSMS GUI method)

Connect to a SQL instance in SSMS and navigate to Management. Expand the Management folder, followed by SQL Server Logs. In the SQL Server Logs, you can see different logs – Current and Archive:

连接到SSMS中SQL实例,然后导航到“管理”。 展开管理文件夹,然后展开SQL Server日志。 在SQL Server日志中,您可以看到不同的日志-当前日志和存档日志:

Double-click on the desired error log file and it will open the error log in a separate window:

双击所需的错误日志文件,它将在单独的窗口中打开错误日志:

T-SQL方法 (T-SQL method)

You can use SERVERPROPERTY() command to know the current error log file location

您可以使用SERVERPROPERTY()命令来了解当前的错误日志文件位置

SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location';

You can browse to the directory and it shows available error log files:

您可以浏览到目录,并显示可用的错误日志文件:

  • ErrorLog: Current error log file ErrorLog:当前错误日志文件
  • ErrorLog.1: Archive error log file 1 ErrorLog.1:存档错误日志文件1
  • ErrorLog.2: Archive error log file 2 ErrorLog.2:存档错误日志文件2

使用GUI访问错误日志的问题 (Problem with accessing error log using GUI)

Usually DBA uses the GUI method for reading the error log as shown above. You might face an issue where you try to open the error log, but it takes much time in opening it. Further, it gives a timeout issue. It might happen due to a huge error log.

通常,DBA使用GUI方法读取错误日志,如上所示。 您可能会遇到尝试打开错误日志的问题,但是打开错误日志会花费很多时间。 此外,它给出了超时问题。 由于错误日志过多,可能会发生这种情况。

  • Usually, we deploy a SQL Agent job to recycle error log files so that it does not grow huge. If the job is not available or not running, it might result in a substantial error log 通常,我们部署一个SQL Agent作业来回收错误日志文件,以便它不会变得很大。 如果作业不可用或未运行,则可能会导致大量错误日志
  • If we have enabled auditing for successful logins, SQL Server logs an entry for each successful database connection. In a busy OLTP system, you might get many successful logins that can flood error logs 如果我们为成功的登录启用了审核,则SQL Server会为每个成功的数据库连接记录一个条目。 在繁忙的OLTP系统中,您可能会获得许多成功的登录信息,这些登录信息会淹没错误日志
  • SQL Server log backups. Suppose you have 50 databases in SQL instance. You configured transaction log backup every 10 minutes. Therefore, every 10 minutes, it writes information about log backups for each database in the error logs. We can suppress these messages, but it is not in the scope of this article SQL Server日志备份。 假设您在SQL实例中有50个数据库。 您每10分钟配置一次事务日志备份。 因此,每隔10分钟,它将在错误日志中写入有关每个数据库的日志备份的信息。 我们可以禁止显示这些消息,但这不在本文讨论范围之内

If we want to search through multiple error logs, it can be a frustrating and challenging task to read and filter logs using GUI.

如果我们要搜索多个错误日志,则使用GUI读取和过滤日志可能是一项令人沮丧且具有挑战性的任务。

查询以列出错误日志及其大小 (Query to list error log and their sizes)

You can execute the following query to list down available error logs along with their sizes:

您可以执行以下查询以列出可用的错误日志及其大小:

EXEC sys.sp_enumerrorlogs;

Here the date column shows the timestamp of last entry in the particular file.

这里的日期列显示了特定文件中最后一个条目的时间戳。

使用xp_readerrorlog读取SQL Server日志文件 (Read SQL Server log files using xp_readerrorlog)

We use extended stored procedure xp_readerrorlog to read SQL Server error log files.

我们使用扩展存储过程xp_readerrorlog读取SQL Server错误日志文件。

xp_ReadErrorLog命令的语法 (Syntax for xp_ReadErrorLog command)

Exec xp_ReadErrorLog  <LogNumber>, <LogType>, <SearchItem1>, <StartDate>, <EndDate>, <SortOrder>
  • LogNumber: It is the log number of the error log. You can see the LogNumber:错误日志的日志号。 您可以在上面的屏幕截图中看到lognumber in the above screenshot. Zero is always referred to as the current log file 日志编号 。 零始终称为当前日志文件
  • LogType: We can use this command to read both SQL Server error logs and agent logs LogType:我们可以使用此命令读取SQL Server错误日志和代理日志
    • 1 – To read the SQL Server error log 1 –读取SQL Server错误日志
    • 2- To read SQL Agent logs 2-读取SQL Agent日志
  • SearchItem1: In this parameter, we specify the search keyword SearchItem1:在此参数中,我们指定搜索关键字
  • SearchItem2: We can use additional search items. Both conditions ( SearchItem1 and SearchItem2) should be satisfied with the results SearchItem2 :我们可以使用其他搜索项。 结果都应满足两个条件(SearchItem1和SearchItem2)
  • StartDate and EndDate: We can filter the error log between StartDate and EndDate StartDate和EndDate:我们可以过滤StartDate和EndDate之间的错误日志
  • SortOrder: We can specify ASC (Ascending) or DSC (descending) for sorting purposes SortOrder:我们可以指定ASC(升序)或DSC(降序)进行排序

Let’s understand command xp_readerrorlog using the following examples.

让我们使用以下示例了解命令xp_readerrorlog。

读取当前错误日志 (Read Current error log)

We can read the current SQL Server error log without specifying any parameter:

我们可以在不指定任何参数的情况下读取当前SQL Server错误日志:

EXEC xp_ReadErrorLog

读取SQL Server错误日志 (Read SQL Server error log)

In the following command, we use the following parameters:

在以下命令中,我们使用以下参数:

  • 0: To specify the current error log file 0:指定当前错误日志文件
  • LogType as specified above LogType指定一个值,如上所述

读取SQL Server代理日志 (Read SQL Server agent log)

In this command, we specify a value for LogType parameter 2 that refers to SQL Server agent logs:

在此命令中,我们为LogType参数2指定一个值,该值引用SQL Server代理日志:

EXEC xp_ReadErrorLog 0,2

读取当前SQL Server错误日志并搜索文本“警告” (Reads current SQL Server error log and search for text ‘Warning’)

This command searches for text warning in the current SQL Server error log file:

此命令在当前SQL Server错误日志文件中搜索文本警告:

EXEC xp_ReadErrorLog 0, 1, N'Warning'

We need to put the text in single quotes with N. We might get following error message with N. It declares the string as Nvarchar data type instead of Varchar:

我们需要使用N将文本放在单引号中。我们可能会收到带有N的以下错误消息。它将字符串声明为Nvarchar数据类型,而不是Varchar:

Msg 22004, Level 12, State 1, Line 0
Error executing extended stored procedure: Invalid Parameter Type

消息22004,级别12,状态1,第0行
执行扩展存储过程时出错:无效的参数类型

读取当前SQL Server错误日志并搜索多个条件 (Reads current SQL Server error log and search for multiple conditions)

This query searches for text Database and Initialization in the current SQL Server error logs. Both the conditions should satisfy to return results:

此查询在当前SQL Server错误日志中搜索文本数据库和初始化。 这两个条件都应满足以返回结果:

EXEC xp_ReadErrorLog 0, 1, N'Database',N'Initialization'

在特定持续时间内读取当前SQL Server错误日志 (Read current SQL Server error log for a specific duration)

Suppose we want to search SQL Server error logs for a specific duration. Suppose we want to search for keyword recovery between 7th November 2019 midnight and 9 AM. We can specify the start and end date parameter in the following format:

假设我们要搜索SQL Server错误日志特定的持续时间。 假设我们要搜索2019年11月7日午夜至9 AM之间的关键字恢复。 我们可以采用以下格式指定开始日期和结束日期参数:

EXEC xp_readerrorlog 0, 1, N'Recovery', N'', N'2019-11-07 00:00:01.000', N'2019-11-07 09:00:01.000'

In the output, you can see the result is sorted in the ascending order from the start date to the end date. We can specify parameter desc so that results are sorted in descending order from an end date to start date:

在输出中,您可以看到结果从开始日期到结束日期按升序排序。 我们可以指定参数desc,以便将结果从结束日期到开始日期按降序排序:

EXEC xp_readerrorlog 0, 1, N'Recovery', N'', N'2019-11-07 00:00:01.000', N'2019-11-07 09:00:01.000',N'desc'

使用变量读取特定时间段内的多个条件的当前SQL Server错误日志 (Read current SQL Server error log for a specific duration, multiple conditions using variables)

We can use the following query for specifying the required parameter in the variable and use these variables in the command xp_readerrorlog.

我们可以使用以下查询在变量中指定所需的参数,并在命令xp_readerrorlog中使用这些变量。

It uses the following conditions:

它使用以下条件:

  • Search keywords Recovery and MSDB 搜索关键字Recovery和MSDB
  • Specify start and end date 指定开始和结束日期
DECLARE @logFileType SMALLINT= 1;
DECLARE @start DATETIME;
DECLARE @end DATETIME;
DECLARE @logno INT= 0;
SET @start = '2019-11-07 00:00:01.000';
SET @end = '2019-11-07 09:00:00.000';
DECLARE @searchString1 NVARCHAR(256)= 'Recovery';
DECLARE @searchString2 NVARCHAR(256)= 'MSDB';
EXEC master.dbo.xp_readerrorlog @logno, @logFileType, @searchString1, @searchString2, @start, @end;

结论 (Conclusion)

This article explores the methods for reading SQL Server error logs using xp_readerrorlog command. It helps to troubleshoot issues in SQL Server and avoid the pain of reading large error logs from GUI for specific conditions.

本文探讨了使用xp_readerrorlog命令读取SQL Server错误日志的方法。 它有助于对SQL Server中的问题进行故障排除,避免因特定情况而从GUI读取大型错误日志的麻烦。

翻译自: https://www.sqlshack.com/read-sql-server-error-logs-using-the-xp_readerrorlog-command/

使用xp_readerrorlog命令读取SQL Server错误日志相关推荐

  1. 把SQL Server 错误日志导出为EXCEL 并发送到指定的ftp 或者 共享盘

    /* 2005版本 和2000 版本 sql server 错误日志结果不同. 下面是 适用于 SQL2000的 其中加入了 自己编写的一个ftp小程序 用来上传 相关日志状况 */ IF objec ...

  2. 读取SQL Server事务日志

    介绍 (Introduction) There has always been some debate as to whether or not there are real benefits to ...

  3. SQL Server自动化运维系列——监控磁盘剩余空间及SQL Server错误日志(Power Shell)...

    原文:SQL Server自动化运维系列--监控磁盘剩余空间及SQL Server错误日志(Power Shell) 需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的 ...

  4. 如何使用损坏或删除SQL Server事务日志文件重建数据库

    This is the last article, but not the least one, in the SQL Server Transaction Log series. In this s ...

  5. SQL Server事务日志体系结构

    This article will cover SQL Server transaction log architecture including file topography, basic ove ...

  6. SQL Server事务日志备份,截断和缩减操作

    In this article, we will cover SQL Server Transaction log backups, truncate and shrink operations wi ...

  7. SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转)...

    前言: 今天遇到这个sql服务无法启用 .无法登陆 的情况..在google 百度 搜了一下.发现很多网站都是采集来的数据..(很奇怪这些采集站都那么靠前!) 照着文章里边的方法去试试都不行,,,后来 ...

  8. SQL Server事务日志采访问题

    In this article, we will discuss some important SQL Server Transaction Log interview questions that ...

  9. SQL Server事务日志管理的阶梯,第5级:在完全恢复模式下管理日志

    SQL Server事务日志管理的阶梯,第5级:在完全恢复模式下管理日志 在本节中,我们将回顾在完全恢复模式下进行日志备份的原因和方法,以及如何使用这些日志备份文件与完整的数据库备份一起执行数据库恢复 ...

最新文章

  1. php在用js_encode()转换时,实现斜杠不转义
  2. 决策树 bagging boosting 的区别
  3. 关于oracle数据库的操作的命令
  4. VTK:隐式函数之SampleFunction
  5. pythonui自动化测试平台_django+appium实现UI自动化测试平台(开源部分,可定制开发)...
  6. P2:图像分类:KNN与线性分类器
  7. ORALCE 两表结构更新
  8. 这就是为什么上帝和魔鬼都喜欢数学......
  9. (copy即运行)嗖嗖移动大厅详讲!!!一看便知
  10. 解决fbreader从书架打开的是同一本书问题
  11. RabbitMQ高级特性-惰性队列
  12. 让图片动起来——Real_Time_Image_Animation
  13. 飞书接入ChatGPT - 将ChatGPT集成到飞书机器人,直接拉满效率
  14. WML 中文参考手册
  15. virtualenv虚拟环境中安装MySQL-python
  16. 机器人能源处理专题-机器人电源管理系统
  17. 洛谷P2404 自然数的拆分问题(回溯)
  18. 图像处理之opencv库使用小结
  19. php去重和 sql 去重,原生php结果集如何去重?
  20. 京东MySQL数据库Docker化最佳实践(附PPT)

热门文章

  1. Swift 类的使用class
  2. 【C#】使用DWM实现无边框窗体阴影或全透窗体
  3. 通过VirtualBox与Vagrant在windows上搭建LNMP开发环境
  4. 打造Android微信朋友圈下拉刷新控件
  5. [转载] 分布式系统trace实践
  6. WPF 位置转化和动画
  7. WCF创建Rest服务(附:.net2.0创建Rest服务)
  8. Google的十个核心技术
  9. LeetCode(728)——自除数(JavaScript)
  10. 闭包的理解、缺点以及应用场景