7

A very short and straight question: I want to catch the error using try..catch and log it into a file. Is this possible in SQL Server 2008?

Any directions, help are welcome.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manish
  • 6,106
  • 19
  • 64
  • 90

3 Answers3

8

Yes, you can.

Just implement try catch as it's described here TRY...CATCH . Error logging can be logged either in application or in sql by writing errors to a table.

If you want to log into a file, you can do that using SQLCLR. Check the answer here

How to log in T-SQL

There're some similar questions you can check.

Logging into table in SQL Server trigger

Best Practices - Stored Procedure Logging

Another approach is to use Log4Sql

Community
  • 1
  • 1
hgulyan
  • 8,099
  • 8
  • 50
  • 75
1

View the SQL Server error log by using SQL Server Management Studio or any text editor.

By default, the error log is located at Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG and ERRORLOG.n files.

Go through Viewing the SQL Server Error Log page

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

Like this, not exactly to file, but to eventlog, I think writing to file is only possible with CLR procedure:

BEGIN TRY
  Do some error
END TRY
Begin CATCh
  Declare @error nvarchar(max) = error_message()+' your data'
  exec master..xp_logevent 50001, @error, 'error'
  --Notify host application
   RAISERROR(@error, 16, 1) 
END CATCH
Oleg Dok
  • 21,109
  • 4
  • 45
  • 54