How to log in T-SQL

2019-01-22 21:55发布

I'm using ADO.NET to access SQL Server 2005 and would like to be able to log from inside the T-SQL stored procedures that I'm calling. Is that somehow possible?

I'm unable to see output from the 'print'-statement when using ADO.NET and since I want to use logging just for debuging the ideal solution would be to emit messages to DebugView from SysInternals.

10条回答
我想做一个坏孩纸
2楼-- · 2019-01-22 22:15

For what it's worth, I've found that when I don't assign an InfoMessage handler to my SqlConnection:

sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(MySqlConnectionInfoMessageHandler);

where the signature of the InfoMessageHandler looks like this:

MySqlConnectionInfoMessageHandler(object sender, SqlInfoMessageEventArgs e)

then my PRINT statements in my Stored Procs do not appear in DbgView.

查看更多
Ridiculous、
3楼-- · 2019-01-22 22:18

You may want to check Log4TSQL. It provides Database-Logging for Stored Procedures and Triggers in SQL Server 2005 - 2008. You have the possibility to set separate, independent log-levels on a per Procedure/Trigger basis.

查看更多
淡お忘
4楼-- · 2019-01-22 22:20

You can either log to a table, by simply inserting a new row, or you can implement a CLR stored procedure to write to a file.

Be careful with writing to a table, because if the action happens in a transaction and the transaction gets rolled back, your log entry will disappear.

查看更多
萌系小妹纸
5楼-- · 2019-01-22 22:23

Logging from inside a SQL sproc would be better done to the database itself. T-SQL can write to files but it's not really designed for it.

查看更多
登录 后发表回答