Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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)
 
Share this answer
 
Comments
HighOnFireZA 2-Dec-11 4:50am    
Excellent, I wasn't aware of the Log property. I will check this out. Thanks
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.
 
Share this answer
 
Comments
HighOnFireZA 2-Dec-11 5:37am    
Thanks for the suggestion but profiler won't be applicable in this scenario.
AmitGajjar 2-Dec-11 22:21pm    
Yes my mistake, you need datacontext.log to log all the sql query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900