You can view error logs in SQL Server Management Studio (SSMS). To view SQL Server-specific errors, you can open up SSMS and expand the SQL Server Agent node. Under the Error Logs folder, you can see the most recent error and the most recent archived error:
Alternatively, you can go to Management and expand SQL Server Logs. When you double-click on any one of the error logs, a log file viewer appears. You can select an option to view additional logs from the left-hand side pane. To view the most recent SQL Server-specific logs, check SQL Server on the left-hand side pane and then the Current checkbox:
Using SMO, you can also query this same information. This requires creating an SMO server object that references the instance you are working with. Once your server variable is instantiated, you can invoke the ReadErrorLog()
method. The following snippet shows how you can display the five most recent entries in the SQL Server log:
#assuming you already created your SMO server...