Linq to Sql Profiler: Spike results

time to read 3 min | 478 words

Well, so far, so good.

I started by defining a simple Linq to SQL model, there are zero things that you need to do here to make things work:


And now to the actual code using this model:

static void Main()
    using (var db = new BlogModelDataContext())
        var q = from blog in db.Blogs
                where blog.Title == "The Lazy Blog"
                select blog;

        foreach (var blog in q)

            foreach (var post in blog.Posts)

I think that we can agree that this is pretty routine usage of Linq to SQL. The only thing extra that we need is to initialize the profiler endpoint.

The end result is:


We can detect data contexts opening and closing, we can detect queries and their parameters, format them to display properly and show their results. We can even detect queries generated from lazy loading and the stack trace that caused each query (a hugely valuable feature).

Now, before you get too excited, this is a spike. A lot of the code is going to end up in the final bits, but there is a lot more to do yet.

Things that I am not going to be able to do:

  • Track local transactions (I’ll probably be able to track distributed transactions, however)
  • Show query row count
  • Track query duration
  • Show entities load by session

I am going to be able to show at least some statistics, however, which is pretty nice, all told.