views:

63

answers:

1

I'm trying to create a logger that will write the queries LINQ executes to the page formatted with the great javascript library SyntaxHighlighter.

For that, I've set the DataContext's Log property to my custom logger, which is working well.

Now I just need to get the current Controller object (outside the controller execution context) so I can set some ViewData with what needs to be outputted.

Any suggestions?

+2  A: 

If you want to perform operations outside of the controller, then action-filters (etc) are a good bet; just inherit from ActionFilterAttribute, override OnActionExecuting (etc) to inject data into the view, and mark your controller with [YourCustomFilter].

Like this


(original; I may have misunderstood)

It would be better to use dependency injection here, by passing the log-writer into the repository as a constructor argument (ideally via something like StructureMap, which works very well with MVC via StructureMapControllerFactory, or similar).

Marc Gravell
That wouldn't solve my problem. I can get the SQL queries that LINQ generates, I just need to find a way to get them to the page.
changelog
I won't be able to get the logger instance like that though, unless I make it singleton, because I can't extend it from TextWriter and ActionFilterAttribute at the same time.
changelog
Then maybe the base-controller approach is more appropriate; if the base-controller had a (protected) TextWriter that you could pass down into the DAL?
Marc Gravell
I did it with singleton. Works like a charm, in a popup window. Might consider releasing the code :)
changelog