LINQ to SQL Logging

Posted Thursday, February 21, 2008 9:24 AM by CoreyRoth

The other day I was reading Charlie Calvert's excellent post on LINQ and Deferred Execution and stumbled upon a logging feature inside LINQ to SQL.  By setting the Log property to a TextWriter (in this case Console.Out), LINQ to SQL will log the query and what parameters it sent to the database server.  Assign it before you execute your query like this.

MyDataContext.Log = Console.Out;

Obviously you don't have to log to the console, you can log to any text writer.  This provides a great way to log what queries are going to the database and helps eliminate the mystery of what queries have been executing in your application.  Obviously, you can view these queries in the debugger, but once your application is in production, this is a good solution.

Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)