通过DataContext记录实际的SQL语句 [英] Log actual SQL statement through DataContext

查看:150
本文介绍了通过DataContext记录实际的SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在忙于使用DataContext从数据库查询数据的相对较大的应用程序.当前,我们具有集中的错误处理和日志记录功能,但问题是,如果存在sql异常,则它不会记录实际的SQL查询.

是否有任何简单的方法可以在不使用我们每次使用DataContext的情况下都将其记录下来的情况下集中实现呢?
我们有一个从我们使用的DataContext继承的类.在将SQL语句通过此类传递到实际的DataContext时捕获它们是理想的选择,但是对于Linq来说这有点困难,因为它没有字符串SQL命令.

任何建议将不胜感激.

Hi All

I''m busy working on a relatively big application that is using a DataContext to query data from the database. We currently have central error handling and logging but the problem is that if there is a sql exception, it doesn''t log the actual SQL query.

Is there any easy way of implementing this centrally without logging it at each instance we use the DataContext?

We have a class that inherits from DataContext that we use. It would be ideal to catch the SQL statements as they pass through this class to the actual DataContext but it''s a bit difficult with Linq as there is no string SQL command.

Any suggestions would be greatly appreciated.

推荐答案

在从DataContext继承的类中,在初始化期间,您不仅可以将上下文Log属性设置为您选择的日志记录提供程序?

http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.log.aspx [ ^ ]

本文显示了与log4Net一起使用

将LINQ重定向到SQL DataContext日志消息到Log4Net [
In your class that inherits from DataContext, during initialisation can you not just set the context Log property to a logging provider of your choice?

http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.log.aspx[^]

This article shows using with log4Net

Redirect LINQ to SQL DataContext Log Messages To Log4Net[^]

The nice thing about log4Net is that it''s easily configurable, you could switch on\off SQL logging with a simple config change & you can redirect the output to numerous destinations (text file, email, SQL database etc)




另一个解决方案是,如果您已安装SQL Server,则可以使用SQLProfiler来检查在数据库上触发的sql查询.

SQLProfiler可能对您有帮助.

谢谢
-amit.
Hi,

another solution is , if you have installed SQL server then there is SQLProfiler available for checking sql query fired on databases.

SQLProfiler can be helpful to you.

thanks
-amit.


这篇关于通过DataContext记录实际的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆