Wow, I just learnt a valuable trick in LINQ 2 SQL.
Set your DB Data Context “Log” property to “Console.Out” and once a query has been executed by LINQ 2 SQL, the query executed is outputted to console! This is great, but I straight away would want this sending to a debug window or if you are after further flexibility, you can output to a file, memory or multiple output writers with the below code:
class DebugTextWriter : System.IO.TextWriter {
public override void Write(char[] buffer, int index, int count) {
System.Diagnostics.Debug.Write(new String(buffer, index, count));
}
public override void Write(string value) {
System.Diagnostics.Debug.Write(value);
}
public override Encoding Encoding {
get { return System.Text.Encoding.Default; }
}
}
To use it then simply:
#IF DEBUG myDataContext.Log = new DebugTextWriter(); #endif
^^ I personally use the “#IF DEBUG” line that way this only occurs in debug mode, but it is up to you.
Major thanks to DamienG over at his blog!
One Comment
Thank you for that short version!
I saw a lot of other implementations with at least 100 lines of code!
I would decorate the DebugWriter with
[Conditional("DEBUG")]
instead of using
#IF DEBUG
THANKS
.peter.gfader.
One Trackback/Pingback
Log LINQ 2 SQL query execution to console/debug window « {Programming} & Life…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
Post a Comment