Как посмотреть лог ошибок sql

Ingenious Guide to View Log File of SQL Server

SQL Server Logs

Microsoft SQL Server application is one of the biggest waves in the relational database management system and handles huge databases in a well-structured manner. However, when it comes to viewing SQL Server log files, some users face trouble. We are sure that you must be aware of how good the MS SQL Server database is for both beginners as well as expert users.

But, Digital Crimes Can Takes Place In SQL Server Too!!!!

Nowadays, Ex-employees or hackers intentionally modifies the values of databases in order to damage the organization assets. And it becomes difficult to analyze or examine who is the culprit manually. As a result, the Organizations run into  big trouble.

Hold On!! There Is A Good News

In SQL Server, there is a transaction Log file that keep records of all transactions & modifications in database executed on a database in a Microsoft SQL Server. By reading the Log file, one can easily check who deleted data from table in SQL Server database. Plus, it is used by forensic investigator to examine SQL Server Transaction Log and view & check every log detail in a detailed manner. In short, with SQL Log file, it becomes easy to find out which query performed on which table at what time.

Here, we are going to answer how to view log file of SQL Server by using various workarounds. Just go through this article once and understand how to open or read transaction log file in Microsoft SQL Server 2017 / 2016 / 2014 / 2012 / 2008 / 2008 R2 / 2005.

Moreover, if user want to restore the deleted query from a log file, then they can go through this blog – How to Recover Data from Log file in SQL Server – A Complete Guide .

Now, first of all, users need to find out the location of the log files. Then only they can proceed with a desired solution. So, to understand where your log files are, simply follow the below steps:

  • Open SSMS & Connect to SQL Instance.
  • Go to Management >> SQL Server Logs.
  • Now, View the Log & Archive Logs simply.

logs location

Methods Use For How to View Transaction Log File of SQL Server

In the following section, you will understand how to open, check and read transaction file to retrieve information about the data which had been altered.  Before we move on towards the solution, let’s look at the ways to find the location of SQL logs easily. So, let’s get started!!

As users saw the location of the log files, let’s move ahead. From the start of SQL Server or manual log file recycling, users can use these logs to view recent activities.

1st Method – xp_readerrorlog() function

Users can use the extended xp_readerrorlog process to find the current location of the log file.

USE master
GO
xp_readerrorlog 0, 1, N'Logging SQL Server messages', NULL, NULL,NULL
GO

Now, we have some predefined guidelines for this procedure as mentioned below:

  • End-time
  • from time
  • Search string1
  • Search string 2
  • Sort results – Ascending (N’ASC) or descending (N’Desc)
  • Error log file: value 0 for the current, 1 for Archive#1, 2 for Archive #2
  • Logfile type: Value 0 for SQL Server error log, 1 for SQL Server Agent log

Just as an example: The log file location is C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Log\ERRORLOG. Now users can see these files easily.

xp function

2nd Method – SERVERPROPERTY() function

Search the SERVERPROPERTY query to find the location of your SQL Server Log files.

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

server property function

#Approach 1: Use Log File Viewer in SQL Server Management Studio

Basically, this method exclusively used to open and view the information about following logs in SSMS:

  • Audit Collection
  • Database Mail
  • Job History
  • Data Collection
  • SQL Server
  • SQL Server Agent
  • Windows Events

Its prime function of Log File Viewer is to provide the report of activities taken place in SQL Server Management Studio. In fact, one can open the Log File Viewer wizard in different ways on the basis of  information that you want to check. Now, go through the instructions to view log details in SQL Server.

How to View Log File of SQL Server Via. Log File Viewer

Step-1. Open Microsoft SQL Server Management Studio application. Here, we are using SQL Server 2014 environment for reading SQL Server Error Log.

Open SSMS

Step-2. Connect to Server windows pops-up. Here, you need to select the Server Name and Type of Authentication. Afterward, click on Connect.

Establish Connection

Step-3. In Object Explorer, go to Management as shown in the screenshot to examine or read log file of SQL Server 2014.

Choose Management

Step-4. Now, move to SQL Server Logs option.

read transaction log file of sql server 2008 r2

Step-5. Now, Right-click on SQL Server Logs and select View >> SQL Server Log sequentially.

Step-6. All the Log summary displayed on Log File Viewer window. Here, you can select other logs such as SQL Server Agent , Database Mail from the left panel to check its information too.

Select View to read error log file

#Approach 2: View Log File of SQL Server Via. Undocumented fn_dblog()

Originally, the function fn_dblog() is used to extract data from Transaction file of SQL Server for forensic purposes to analyze every log event performed on the table. So, let’s check out how to read transaction log file in Microsoft SQL Server 2017 / 2016 / 2014 / 2012 / 2008 / 2008 R2 / 2005 editions.

Steps to View Log File in SQL Server Using Fn_dblog()

Step 1: We have a table named as ‘Employee’. So, first view the values of the table using the following T-SQL.
Select * from employee.

View Table Data

Step 2: Afterward, alter the table data using update command. For this, execute the query;
Update employee set department ='IT' where emp_name = 'jeevan'

Alter the table

Step 3: Again, view the table values using the Select Query. Now, you can see a modified table.

Updated Table

Step 4: Run the fn_dblog function according to the need. Here, we execute the query to check out the time when update operation was executed.

Select [Begin Time], [Transaction Name] from fn_dblog(null , null) where [Transaction Name] = ‘Update’

read transaction log file sql server

Step 5: In a situation, when you want to analyze all the logs such as Delete etc. , then run the following T-SQL query.

Select [Begin Time], [Transaction Name] from fn_dblog(null, null)

However, there are some consequences attached with fn_dblog(). Actually, this function only provide the time of the query when it was committed instead of which data entry gets affected. Due to which, it becomes cumbersome to find out which table data get altered.

This problem is overcome with the third technique where user can view the log file of SQL Server without any hassle. Apart from this, both the described technique can run in SQL Server Management Studio only. You cannot read a Transaction Log File in offline environment with Log File Viewer and Fn_dblog().

#Approach 3: Use  Smart Solution to Analyze Transaction File Easily

To get exact information from SQL Log File, take the help of SysTools SQL Log Reader Software. With the help of this software, user can scan and analyze T-log file in human readable format. However, the tool works in Online as well as Offline environment. User can get the information like Transaction , Login Name , Time , Table Name , Query . It is a best software solution that answers the question – how to read SQL Server Transaction Log file.

Download Free SQL Log Analyzer

Related : How to Fix Log File Corruption – Step-By-Step Guide

In fact, after viewing the log file of SQL Server, user can export the query in Live SQL Server database environment , SQL Compatible Scripts , and in CSV format. Moreover , the software can read Transaction log file of every SQL Server edition.

Conclusion

That’s all about how to View Log file of SQL Server. Now, go through the above-mentioned methods and opt for the best that is suitable for you and examine the SQL Server Transaction Log file without any hassles at all. Make sure that you are confident while executing the manual method or simply go for the automated solution.

Also Read: SQL Server Transaction Log Forensics

Frequently Asked Questions

How to open SQL Server database Transaction Log file (.ldf) in readable format?

Try SQL Log Analyzer tool to easily scan and read the Transaction .ldf file records.

How to view SQL Log file in SSMS?

Use Fn_dblog() function to read the details of transaction in SQL Server.

Can I read log file of SQL Server 2008?

With the help of SQL Log Viewer, one can read .ldf file and view Transaction, Transaction time, Table name and Query of Microsoft SQL Server 2017, 2016, 2014, 2012, 2008 and SQL Server 2005

Is it possible to examine SQL Transaction Log file?

Yes, with the help of mentioned workaround, one can easily examine SQL LDF file.

April 13, 2019
MSSQL, Windows

SQL Server Logs and Operating System Logs(Event Logs) are logs that SQL Server Database Administrators checks on a daily basis, or use to find the cause of the error when they encounter an error. In this article, we will learn how to check these logs.

Through SSMS, you can access both logs from the View tab by right-clicking SQL Server Logs under Management as follows.

You can also access the operating system logs from the Event Viewer on Windows. To access the event viewer,  go to Run->Search and write “Event Viewer”.

When we open the Event Viewer, a screen appears as below. You can find logs from the tabs under Windows Logs.

If you want to filter SQL Server Error Log, I suggest you to read the below article.

“How To Filter SQL Server Error Log(Like or Not Like)”

Loading

Search code, repositories, users, issues, pull requests…

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

This article covers an overview of SQL Server logs for monitoring and troubleshooting issues in SQL Server.

Introduction

The logs are the best resources for a database administrator in troubleshooting any issues. These issues can be related to server configuration, startup, recovery, performance, trace flags, deadlocks, IO, or Checkpoint delay. For
example, suppose your SQL Server instance restarted due to unknown reasons, and after startup, SQL Services are up;
however, your application cannot access the database. Therefore, to investigate issues, you can look at the latest
SQL Server logs and monitor the database recovery process and estimated time in completion.

The database administrator can also configure SQL Server for additional logging into the error logs. For example, we can enable a trace flag to capture deadlocks information. The DBA should review these logs proactively to look for potential problems. You can identify information such as backup failure, login failure, IO errors by reviewing logs. These error logs are great to look for existing or potential problems in SQL Server instances.

SQL Server logs are known as SQL Server Error logs. This error log has informational, warning, and critical error messages. You can view a few of these logs in the Windows event viewer logs as well. However, it is recommended to use SQL Server logs to get detailed information.

SQL Server Logs and its location

Once you connect to a SQL Server instance in SSMS, navigate to Management -> SQL Server Logs. As shown below, it
has the current log and six archive logs ( Archive#1 to Archive #6).

SQL Server Logs location

Method 1: Using the xp_readerrorlog extended procedure

The current logs are the latest error log file, and you can use them to view recent activity since SQL Server starts
or manual log file recycling. SQL Server error log is a text file stored in the log directory of SQL Server
instance. You can use the extended procedure xp_readerrorlog to find out the current location of
the error log.

USE master

GO

xp_readerrorlog 0, 1, N‘Logging SQL Server messages’, NULL, NULL,NULL

GO

This query has the following parameters:

  1. Error log file: value 0 for the current , 1 for Archive#1, 2 for Archive #2
  2. Logfile type: Value 0 for SQL Server error log, 1 for SQL Server Agent log
  3. Search string1
  4. Search string 2
  5. from time
  6. end-time
  7. Sort results – Ascending (N’ASC) or descending (N’Desc)

For my demo instance, the error log file location is C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Log\ERRORLOG.

xp_readerrorlog extended procedure

Method 2: Using the SERVERPROPERTY() function

We can query the SERVERPROPERTY function as well to identify the location of the SQL Server ERRORLOG.

SELECT SERVERPROPERTY(‘ErrorLogFileName’)  AS ‘Error log location’

SERVERPROPERTY() function

Method 3: Using the SQL Server Configuration Manager

Alternatively, open SQL Server Configuration Manager and view startup parameters. The log file location is specified
using the -e switch.

SQL Server Configuration Manager

You can browse the log directory and view the current archive error log files. These error logs can be opened in a
text editor such as Notepad or Visual Studio Code.

View logs in the LOG directory

Configure the number of SQL Server Log files and their size

By default, SQL Server maintains a current and six archived log files. To validate the value, right-click on the SQL
Server Logs folder in SSMS and Configure.

Configure the number of SQL Server Log file and their size

SQL Server logs all information in the current log file irrespective of log file size. On a busy system or instance
with many errors, you might find it challenging to view the log file in SSMS. SQL Server creates a new log file and
archives the current file in the following case.

  • During SQL Service restart
  • Manual error log recycle

However, if you restart servers frequently due to unknown reasons, you might lose all historical archive log data because it maintains only six archive logs. Since the error contains valuable information that can help you troubleshoot, we might not want to lose this crucial data. Instead, we might want to retain log files on a critical production system for a week or even a month.

SQL Server allows the configuration of up to 6 to 99 error log files. We cannot specify a value less than six because, in any case, it maintains six archive error logs.

To change the default number of error log files, put a check on the box labeled – “Limit the number of error
log files before they are recycled”.
For example, the following screenshot shows a maximum of 30 error
log files.

Limit the number of error log files before they are recycled

Its equivalent T-SQL script uses xp_instance_regwrite extended stored procedure and updates the registry value.

USE [master]

GO

EXEC xp_instance_regwrite N‘HKEY_LOCAL_MACHINE’, N‘Software\Microsoft\MSSQLServer\MSSQLServer’, N‘NumErrorLogs’, REG_DWORD, 30

GO

  • Note: SQL Services must be restarted so that these changes are in effect

As stated earlier, by default, the error log size is unlimited. For example, if you do not start SQL Server for a
longer period and do not manually recycle log files, this file will grow huge. Therefore, in the error log
configuration, value 0 shows log size is unlimited.

Log file size

You can specify the size in KB to limit the error log size as per your requirement. For example, here, we limit log file size to 1 GB

Set Log file maximum size

Its equivalent T-SQL script updates the ErrorLogSizeInKb in the SQL Server registry.

USE [master]

GO

EXEC xp_instance_regwrite N‘HKEY_LOCAL_MACHINE’, N‘Software\Microsoft\MSSQLServer\MSSQLServer’, N‘ErrorLogSizeInKb’, REG_DWORD, 1048576

GO

Manual recycle error logs

SQL Server allows recycling error logs manually to manage them effectively. For example, suppose you increased the number of error log files to 30. Thus, we can create a SQL Server agent job that recycles error logs at midnight. This way, we have an error log file for each day unless SQL Service is restarted in between. To recycle manually, execute the system stored procedure sp_cycle_errorlog. The user with the sysadmin fix server role can execute this stored procedure.

SQL Server Agent log file

SQL Server agent also has a separate error log similar to the SQL Server logs. You can find it under the SQL Server Agent – > Error logs folder.

Manual recycle error logs

Right-click on the Error log folder and Configure. It gives the agent error log location and agent log level.

The agent log file extension is *.OUT and stored in the log folder as per default configuration. For example, in my
system, the log file directory is C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Log\SQLAGENT.OUT.

By default, agent log file logs errors and warnings; however, we can include information messages:

  • Warning messages: These messages provide information about potential problems. For example, “Job X was deleted while it was running”
  • Error message: It gives information that requires immediate intervention of a DBA, such as being unable to start a mail session

SQL Server Agent logs

To add the information message, put a tick on the checkbox labeled Information.

Agent log level

SQL Server uses up to 9 SQL Server agent log files. The current file name is SQLAGENT.OUT. The file with extension .1 indicates the first archived agent error log. Similarly, an extension .9 indicates the 9th (oldest) archived error log.

SQL Server agent log files are recycled each time SQL Server Agent is restarted. To do it manually, right-click on
the Error Logs folder and Recycle.

Recycle agent logs manually

Alternatively, use the stored procedure sp_cycle_agent_errorlog to recycle SQL Server agent log files manually.

USE msdb ;  

GO  

EXEC dbo.sp_cycle_agent_errorlog ;  

GO

The stored procure archives the current agent error log using the following process:

  • A new current agent error log is created
  • Current Agent error log SQLAgent.out coverts to SQLAgent.1
  • SQLAgent.1 converts to SQLAgent.2

Conclusion

The SQL Server error log file has information, warning, and critical messages of an instance. It is helpful for troubleshooting, audit logins (success, failure). The database administrator can configure the number of required archived error logs and directories to store these files.

You should proactively look for messages getting logged as part of your daily and weekly health check reports.

  • Author
  • Recent Posts

Rajendra Gupta

Hi! I am Rajendra Gupta, Database Specialist and Architect, helping organizations implement Microsoft SQL Server, Azure, Couchbase, AWS solutions fast and efficiently, fix related issues, and Performance Tuning with over 14 years of experience.

I am the author of the book «DP-300 Administering Relational Database on Microsoft Azure». I published more than 650 technical articles on MSSQLTips, SQLShack, Quest, CodingSight, and SeveralNines.

I am the creator of one of the biggest free online collections of articles on a single topic, with his 50-part series on SQL Server Always On Availability Groups.

Based on my contribution to the SQL Server community, I have been recognized as the prestigious Best Author of the Year continuously in 2019, 2020, and 2021 (2nd Rank) at SQLShack and the MSSQLTIPS champions award in 2020.

Personal Blog: https://www.dbblogger.com
I am always interested in new challenges so if you need consulting help, reach me at rajendra.gupta16@gmail.com

View all posts by Rajendra Gupta

Rajendra Gupta

In this SQL Server tutorial, we will discuss how to view SQL Server view error logs. Additionally, we will cover the following topics.

  • View SQL Server error logs
  • View SQL Server error log permissions
  • View SQL Server agent error log
  • SQL Server error log file location
  • How to locate SQL Server error log file location
  • How to view SQL Server error log files
  • See the SQL Server error log for more information
  • SQL Server view error log stored procedure

View SQL Server error log permissions

Before we discuss how to view SQL Server Error Log, a user needs to have some permissions. When we talk about permissions related to error logs, there are 2 options in SQL Server.

First is when we are accessing an online SQL Server instance, and send is when we have an offline SQL Server instance.

In the case of an online SQL Server instance, a user needs to be a member of the securityadmin fixed server role. On the other hand, in the case of an offline SQL Server instance, a user needs to have read permission for these 2 things.

  1. Root\Microsoft\SqlServer\ComputerManagement10 WMI namespace
  2. The directory containing the log files, for example, “Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG”

Read How to export data from SQL Server to Excel

In SQL Server, error logs help to verify whether the processes are completed successfully or not. Moreover, these logs also help us to locate any issues in the process.

Now we can easily view or display SQL Server error log by using SQL Server Management Studio or any text editor.

Now, to view the SQL Server Error Logs in SQL Server Management Studio, we can follow the following steps.

  • First, run SQL Server Management Studio and connect to the required database instance.
  • Next, from the Object Explorer, expand the Database instance.
  • After expanding the instance, look for Management Directory and expand it.
  • Under Management directory, look for SQL Server Logs directory and then right-click it and select View > SQL Server Log option.
View SQL Server error logs
View SQL Server error logs
  • This will open a Log File Viewer that will display all the list of error logs
How to view SQL Server error logs
How to view SQL Server error logs

Read SQL Server bulk insert from CSV file

View SQL Server Agent error log

In SQL Server, we generally use SQL Server Management Studio to view the error logs related to any component. Moreover, we specifically use Log File Viewer in the SQL Server Management Studio to display the error logs.

Now, here are the few steps that we can use to view SQL Server Agent error logs.

  • First, run SQL Server Management Studio and connect to the required database instance.
  • Next, from the Object Explorer, first, expand the Database instance and then expand SQL Server Agent directory.
  • Under SQL Server Agent, expand the Error Logs directory and all the error will be listed under this diectory.
  • Now, to view any SQL Server Agent error log, then right-click the log and click on View Agent Log.
View SQL Server Agent error log
View SQL Server Agent error log
  • This action will open a Log File Viewer where all the error logs related to SQL Server were listed. Moreover, we can easily view the log message and its various details.
How to view SQL Server Agent error logs
How to view SQL Server Agent error logs

Read SQL Server function return table

How to view SQL Server error log files

In this section, we will understand how to view SQL Server error log files. Moreover, we will also understand how to locate these error log files.

SQL Server error log file location

As discussed earlier, there are 2 ways in SQL Server to view the SQL Server Error Logs. The first way is to use the Log File Viewer in SQL Server Management Studio. And the second way is to use any text editor.

Now, if we want to view the SQL Server error logs in a text editor then, we need to know the location of these log files. So, in SQL Server, “Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG” is the default location of error log files.

SQL Server error log file location
SQL Server error log file location

Read View in SQL Server

How to locate SQL Server error log file location

In the previous section, we have seen the default location for the SQL Server log files. Now, in this section, we will discuss how to find the location of any SQL Server Error Log.

In SQL Server, there are many methods to implement this task, let’s, discuss these methods in detail.

Using SQL Server Configuration Manager

Here are some of the steps that we can follow to locate the SQL Server Error Log files using SQL Server Configuration Manager.

  • In SQL Server Configuration Manager, under SQL Server Services, right-click SQL Server Service and click on Properties.
  • On the Properties page, open the Startup Parameters page and in this multiple paths are given.
  • Now, the location for the Error logs is given in the one of the Startup Parameters values.
How to locate SQL Server error log file location
Locate SQL Server error log file location

Using SERVERPROPERTY function

As an alternative, we can also use the SERVERPROPERTY function in SQL Server to get the location of the Error Log. The SERVERPROPERTY in SQL Server is a system function that returns information related to various properties of a SQL Server instance.

Hence, we can easily use this function to get the location of the SQL Server Error Log. Moreover, we can execute the following SQL query for this task.

SELECT SERVERPROPERTY('ErrorLogFileName') 
 AS 'SQL Server Error log file location'

Now, the above will not only return the file name but also the complete path location of that error log file.

Locate SQL Server error log file location
Locate SQL Server error log file location

Read Comparison Operators in SQL Server

SQL Server view error log stored procedure

Till now, we have seen how to view the SQL Server Error Log using SQL Server Management Studio. But, in SQL Server, there is also a system stored procedure that returns a list of error logs.

Moreover, we can also use it to view some specific error logs. The name of this stored procedure is SP_READERRORLOG. Let’s understand how to use this store procedure using some examples.

EXECUTE SP_READERRORLOG

First, executing this stored procedure without any parameter will return the list of all error logs. Here is the sample output of the query.

List SQL Server Error Logs using stored procedure
List SQL Server Error Logs using stored procedure

Now, we can also use this store procedure to view a specific error. And for this, we have to use the following syntax.

EXECUTE SP_READERRORLOG <LogNumber>, <LogType>,
                        <SearchTerm1>, <SearchTerm2>

The parameters for using this stored procedure are as follows.

  • LogNumber: it is an integer type parameter used to specify the log number, for example, 0 returns the current log.
  • LogType: it is also an integer type parameter used to specify the log type. For example, value 1 represents SQL Server error logs and 2 represents SQL Server Agent error logs.
  • SearchTerm1: it is an NVARCHAR type parameter used to specify the text to search from the Text column.
  • SearchTerm2: it is also an NVARCHAR type parameter used to specify the second text to search from the Text column.

Now, let’s use this syntax to execute an example in SQL Server. And the query for the example is as follows.

EXECUTE SP_READERRORLOG 0, 1, 'ERROR'

The above query will return the list SQL Server Error Log containing Error text in the Text column. And the result of the query is as follows.

SQL Server view error log stored procedure
SQL Server view error log stored procedure

Read How to see view definition in SQL Server

See the SQL Server error log for more information

Now, if you are working with SQL Server then, you often encounter this alert stating “See the SQL Server error log for more information“. This is because error logs are useful to locate any issues in the process.

Moreover, these logs also help to verify whether the processes are completed successfully or not.

Now, in this tutorial, we have already illustrated multiple methods to view and locate any error log in SQL Server.

You may like SQL server tutorials:

  • SQL Server view order by
  • SQL Server logical operators and example
  • Create a table from view in SQL Server
  • Loop in SQL Server stored procedure
  • Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements
  • Msg 567: Unable to retrieve data for this section of the report
  • Stored procedure for search functionality in SQL Server

So, in this tutorial, we have discussed how to view SQL Server view error logs. Additionally, we have also covered the following topics.

  • View SQL Server error logs
  • View SQL Server error log permissions
  • View SQL Server agent error log
  • SQL Server error log file location
  • How to locate SQL Server error log file location
  • How to view SQL Server error log files
  • See the SQL Server error log for more information
  • SQL Server view error log stored procedure

Bijay

I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.

Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.

Понравилась статья? Поделить с друзьями:
  • Как посмотреть лог ошибок андроид
  • Как посмотреть лог ошибок windows server 2016
  • Как посмотреть лог ошибок project zomboid
  • Как посмотреть лог ошибок mysql
  • Как посмотреть лог ошибок apache