{"id":89608,"date":"2021-01-19T19:58:09","date_gmt":"2021-01-19T19:58:09","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=89608"},"modified":"2022-04-24T16:10:24","modified_gmt":"2022-04-24T16:10:24","slug":"search-sql-server-error-log-files","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/database-administration-sql-server\/search-sql-server-error-log-files\/","title":{"rendered":"Search SQL Server error log files"},"content":{"rendered":"<p>Each instance of SQL Server logs information about its processing to a file known as the error log. Depending on how long an instance has been up and what is being logged, the log files might be small or large. When the log files are small, they are fairly easy to browse using SQL Server Management Studio (SSMS). But when they are large, it is cumbersome to browse through them to find individual error log messages using SSMS. There are even times when the error log file is so large it can\u2019t even be opened up using SSMS. This article will show you a few different ways to browse and search SQL Server error log files.<\/p>\n<h2>Using SSMS to search and filter large SQL Server error log files<\/h2>\n<p>When browsing a large error log file with SSMS, it can take a long time just to scroll through the file to find the portion of the log that you might be interested in reviewing. I find it easier to use the <em>search<\/em> and <em>filter<\/em> options to find the information in large error log files. I\u2019ll demonstrate how to use these options to find information in large error log files.<\/p>\n<h3>Using the search option<\/h3>\n<p>The search option is useful for finding the next occurrence of a string of characters in the log. To search, you can just browse through one of the archived log files, as shown in Figure 1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1020\" class=\"wp-image-89609\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/browsing-my-error-log-file-to-search-sql-server-er.png\" alt=\"Browsing my error log file to search SQL Server error log files\" \/><\/p>\n<p><strong>Figure 1: Browsing my error log file<\/strong><\/p>\n<p>Figure 1 shows the beginning of the error log file, and the entries are sorted by the date\/time from the oldest to the newest. You can see the <em>Search<\/em> function outlined by a red box at the top of the screenshot. To use the search function, just click on this search icon, which brings up the search dialog shown in Figure 2.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"132\" class=\"wp-image-89610\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/search-selection-dialog.png\" alt=\"Search selection dialog\" \/><\/p>\n<p><strong>Figure 2: Search selection dialog<\/strong><\/p>\n<p>To search, just enter the string of characters you want to find in the <em>Search for:<\/em> field. The characters can be case-insensitive or case-sensitive based on whether the <em>Match case<\/em> check box is checked. You can also search just the Message column or all the columns depending on if the <em>Search Message column only<\/em> box is checked. When an error log file spans many days, you could uncheck this checkbox to search for a particular date\/time string in the log. By doing this, the error log can be reposition to display a specific day in the log in a log file that contains multiple days.<\/p>\n<p>For this demonstration, enter the string <em>error<\/em> in the <em>Search for:<\/em> criteria. Once the search criteria are filled in, the <em>Search<\/em> button is enabled, as shown in Figure 3.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"464\" height=\"131\" class=\"wp-image-89611\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/enabling-search-button-search-sql-server-error-log.png\" alt=\"Enabling search button search SQL Server error log files\" \/><\/p>\n<p><strong>Figure 3: Enabling Search Button<\/strong><\/p>\n<p>When clicking the <em>Search<\/em> button, the error log position is relocated to the first occurrence of the string <em>error<\/em>, as shown in Figure 4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1229\" height=\"641\" class=\"wp-image-89612\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/repositioned-to-first-occurrence-of-the-string.png\" alt=\"Repositioned to first occurrence of the string\" \/><\/p>\n<p><strong>Figure 4: Repositioned to first occurrence of the string<\/strong><\/p>\n<p>Click the <em>Search<\/em> button again to move to the next message text that contains the string <em>error<\/em>, as shown in Figure 5.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1223\" height=\"641\" class=\"wp-image-89613\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/next-occurrence-of-the-string-error.png\" alt=\"Next occurrence of the string error\" \/><\/p>\n<p><strong>Figure 5: Next occurrence of the string \u201cerror\u201d<\/strong><\/p>\n<p>By reviewing Figure 5, you can see the search function found the string <em>error<\/em> just a few lines down further in the log (the actual string <em>error<\/em> is located out of view to the right). By clicking the search button repeatedly, you can progressively work through the large error log file finding all the messages that contain the string <em>error<\/em>. Once the last message is found, the search will start over from the top if you click the button again.<\/p>\n<p>Using the search button repeatedly could be a little tedious, especially if the log file contains many messages with string <em>error<\/em>. Another way to find all the messages without clicking and scrolling is to use the filter option.<\/p>\n<h3>Using the Filter Option<\/h3>\n<p>The filter option makes it a little easier to find all the occurrences of a string in the error log file. It does this by sifting through a large error log file and only displaying those rows that meet the filter criteria. Filtering is handy when you want to view specific log entries in a very large log file. To bring up the filter criteria, you need to click on the <em>Filter<\/em> options in the <em>Log File Viewer<\/em> window, as shown in Figure 6.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1094\" height=\"991\" class=\"wp-image-89614\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/selecting-the-filter-option.png\" alt=\"Selecting the filter option\" \/><\/p>\n<p><strong>Figure 6: Selecting the Filter Option<\/strong><\/p>\n<p>When the filter option is clicked, the dialog box in Figure 7 is shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"613\" class=\"wp-image-89615\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/filter-options.png\" alt=\"Filter options\" \/><\/p>\n<p><strong>Figure 7: Filter Options<\/strong><\/p>\n<p>As you can see from Figure 7, there are several different filter selection options from which to choose. You can use one, or more of these filter options to identify those error log records you want to display. Table 1 lists the descriptions for each of these different filter options.<\/p>\n<p><strong>Table 1: Descriptions for each filter option<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p><strong>Filter Name<\/strong><\/p>\n<\/td>\n<td>\n<p><strong>Description<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>User<\/p>\n<\/td>\n<td>\n<p>The user name that is associated with the log entry<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Computer<\/p>\n<\/td>\n<td>\n<p>The computer that is associated with the log entry<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Start Date<\/p>\n<\/td>\n<td>\n<p>Log entry must be created on or after this date<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>End Date<\/p>\n<\/td>\n<td>\n<p>Log entry must be created on or before this date<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Message contains text<\/p>\n<\/td>\n<td>\n<p>Log entry message must contain this text (case-insensitive)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Source<\/p>\n<\/td>\n<td>\n<p>The source of the log entry<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Instance Name<\/p>\n<\/td>\n<td>\n<p>The instance Name that is associated with the log entry<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Event<\/p>\n<\/td>\n<td>\n<p>The event id that is associated with the windows log entry<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To demonstrate how to use the filter dialog to find specific error logs, first try to find the <em>ERRORLOG<\/em> file directory name using the <em>Message contains text<\/em> filter item. The error log directory name is displayed on an error log line item that contains the string <em>Logging SQL Server messages<\/em> in the message text. Therefore, all you need to do is enter this string in the <em>Message contains text<\/em> filter item, check the <em>Apply filter<\/em> checkbox, and then click on the <em>OK<\/em> button, as shown in Figure 8.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"614\" class=\"wp-image-89616\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/applying-filter.png\" alt=\"Applying filter\" \/><\/p>\n<p><strong>Figure 8: Applying Filter<\/strong><\/p>\n<p>After clicking the <em>OK<\/em> button, only the error log lines that contain the text are displayed, as shown in Figure 9. If the <em>Apply Filter<\/em> checkbox is not checked, before clicking on the <em>OK<\/em> button, the filter won\u2019t be applied.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1616\" height=\"805\" class=\"wp-image-89617\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/word-image-63.png\" \/><\/p>\n<p><strong>Figure 9: Results of message text filter<\/strong><\/p>\n<p>Using the filter item is especially useful for finding those messages that are hidden amongst all the messages you are not interested in. I also find using the <em>Start Date<\/em> and <em>End Date<\/em> filters extremely useful to find log entries for a specific date range. The date range filter is handy when the error log file is very large and contains multiple days of error log records.<\/p>\n<h2>Out of memory errors when viewing large logs<\/h2>\n<p>If SQL Server has been up for a while and the error log has not been cycled, or a lot of messages have been written to the log file over a short time, then the error log might be very large &#8212; possibly in the gigabyte size range. If you try to open one of these gigabyte log files using SSMS, a memory exception will occur. Figure 10 shows the out of memory exception that can occur when opening one of the large error log files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"205\" class=\"wp-image-89618\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/word-image-64.png\" \/><\/p>\n<p><strong>Figure 10: Out of memory exception when trying to view a large error log file<\/strong><\/p>\n<p>I got this error when I tried to open one of my large, archived log files that was over 8 GB in size. When this error occurred, some of my log records were loaded into the viewer. I could still use the search option, but I got another memory exception when I tried to use the filter option.<\/p>\n<p>If you are trying to use the SSMS to view large log files and having memory issues, this doesn\u2019t mean you are out of luck. There are other options to view, search and filter these large log files.<\/p>\n<h2>Using a text editor to view a large log file<\/h2>\n<p>One option to view a large log file is to use a text editor. But it can\u2019t just be any text editor; it needs to be a text editor that can read a large file. I have downloaded and used UltraEdit in the past to open large error log files. I\u2019m not endorsing UltraEdit; I only mention it here because it is one of the editors I have used in the past to look at large log files. Keep in mind that UltraEdit is not free software; you need to have a license to use this product long-term. Before you consider downloading any text editor off the internet, make sure you understand the software&#8217;s uses and license requirements being downloaded.<\/p>\n<h2>Programmatically searching the error log file<\/h2>\n<p>Another option for searching those larger log files is to do it programmatically. SQL Server provides an undocumented extended stored procedure named <code>xp_readerrorlog<\/code> that can be used to search the error log and the SQL Agent log files.<\/p>\n<p>Listing 1 is an example of how I used this undocumented stored procedure to search the active error log file on one of my instances of SQL Server.<\/p>\n<p><strong>Listing 1: Using xp_readerrorlog to find the location of error log file<\/strong><\/p>\n<pre class=\"lang:tsql theme:ssms2012-simple-talk\">exec xp_readerrorlog 0,1,N'Logging SQL Server messages in file';<\/pre>\n<p>This example searches for the string <em>Logging SQL Server messages in file<\/em> in the active log file. The output shown in Figure 11 is returned when running the command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"96\" class=\"wp-image-89619\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/01\/word-image-65.png\" \/><\/p>\n<p><strong>Figure 11: Output from running code in Listing 1<\/strong><\/p>\n<p>The log record that identified the file location where the error log messages are being written can be found by searching for this particular string in the active log file.<\/p>\n<p>Even though this stored procedure is undocumented, there are many resources out there that explain how to use it. This stored procedure supports seven parameters. Those parameters are described in Table 2.<\/p>\n<p><strong>Table 2: Parameters for xp_readerrorlog <\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p><strong>Parameter<\/strong><\/p>\n<\/td>\n<td>\n<p><strong>Description<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>1<\/p>\n<\/td>\n<td>\n<p>Identifies the error log file that you would like to read.\u00a0 Set this parm to 0 if you\u2019d like to read the current error log.\u00a0 Or you can set it to either 1, 2, 3, etc. to read one of the historical error log files.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>Identifies which error log to search.\u00a0 1, or null for ERRORLOG, or 2 for the SQL Agent log<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>3<\/p>\n<\/td>\n<td>\n<p>The first string you want to search for in the error log file.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>4<\/p>\n<\/td>\n<td>\n<p>The second string you want to search for in the error log file.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>5<\/p>\n<\/td>\n<td>\n<p>The start time constraint on searching.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>6<\/p>\n<\/td>\n<td>\n<p>The end time constraint on searching.\u00a0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>7<\/p>\n<\/td>\n<td>\n<p>Sort order of the output (ascending, descending)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Finding all the records in a large log file that contained the word <em>error<\/em> can easily be done by just changing the search string in parameter 3 of the code in Listing 1. You can write a short T-SQL script to find all the log records from the active SQL Server log file for yesterday and then place them in a temporary table for further analysis using the code in Listing 2.<\/p>\n<pre class=\"lang:c# theme:vs2012 \">-- Declare Variables needed\r\nDECLARE @StartDate date,\r\n        @EndDate   date;\r\n-- Create temporary table to how error log records\r\nCREATE TABLE #ErrorLogForYesterday (\r\n  LogDate datetime, \r\n  ProcessInfo varchar(max), \r\n  Text varchar(max));\r\nSET @StartDate = dateadd(dd,-1,getdate()); -- Yesterdays Date\r\nSet @EndDate = getdate(); -- Todays Date\r\n-- Extract error log records for yesterday in to temporary table \r\nINSERT INTO #ErrorLogForYesterday EXEC xp_readerrorlog \r\n            0,1,N'',N'',@StartDate,@EndDate;\r\n-- Display error log records extracted\r\nSELECT * FROM #ErrorLogForYesterday;\r\n-- Cleanup\r\nDROP TABLE #ErrorLogForYesterday;<\/pre>\n<p><strong>Listing 2: Code to extract yesterday\u2019s error log records<\/strong><\/p>\n<p>Programmatically finding error log records makes it easy to build processes to analyze the error log file. Using the method in Listing 2, a DBA could create a series of scripts that could programmatically run the xp_readerrorlog stored procedure to quickly analyze the different error log files.<\/p>\n<h2>Reading and Searching SQL Server Error Log Files<\/h2>\n<p>When SQL Server creates large error log files, it presents challenges for DBAs to read them. Large log files are cumbersome to scroll through to find errors. Luckily, the log view functionality of SSMS has the <em>Filter<\/em> and <em>Search<\/em> features built-in to allow a DBA to find strings within these large log files quickly. Additionally, using TSQL code to call the undocumented <code>xp_readerrorlog<\/code> stored procedure, allows a DBA to build scripts to read those large log files. Using these different methods to find errors in large SQL Server log files is critical for managing and maintaining SQL Server.<\/p>\n<p><em>If you like this article, you might also like<\/em>\u00a0<a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/database-administration\/sql-server-error-log\/\">SQL Server Error Log Configuration &#8211; Simple Talk (red-gate.com)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SQL Server error log is helpful for troubleshooting issues. Greg Larsen demonstrates several ways to search SQL Server error log files.&hellip;<\/p>\n","protected":false},"author":78478,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143527],"tags":[136045,136047,5842,136046],"coauthors":[11330],"class_list":["post-89608","post","type-post","status-publish","format-standard","hentry","category-database-administration-sql-server","tag-error-log","tag-search-sql-server-erro-r-log","tag-sql-monitor","tag-sql-server-error-log"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/89608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/78478"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=89608"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/89608\/revisions"}],"predecessor-version":[{"id":89621,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/89608\/revisions\/89621"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=89608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=89608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=89608"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=89608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}