使用xp_readerrorlog命令读取SQL Server错误日志

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日志中,您可以看到不同的日志-当前日志和存档日志:

SQL Server error logs

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

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

View error log in SSMS

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';

Error log 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

View error log in the log folder

使用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;

Query to list error log and their sizes

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

Read Current error log

读取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指定一个值,如上所述

Read SQL Server error log

读取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

Read SQL Server agent log

读取当前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'

Reads current SQL Server error log and search for text '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行
执行扩展存储过程时出错:无效的参数类型

Error message
读取当前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'

search for multiple conditions

在特定持续时间内读取当前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'

Server error log for a specific duration

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'

Server error log for a specific duration in desending order

使用变量读取特定时间段内的多个条件的当前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;

Read current SQL Server error log using variables with xp_readerrorlog command

结论 (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/

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值