views:

31

answers:

2

Hi, I'm starting to develop a new asp.net application based on subsonic3 (for queries) and log4net (for logs) and would like to know how to interface subsonic3 with log4net so that log4net logs the underlying sql used by subsonic.

This is what I have so far:

public static IEnumerable<arma_ocorrencium> ListArmasOcorrencia()
    {
        if (logger.IsInfoEnabled)
        {
            logger.Info("ListarArmasOcorrencia: start");
        }

        var db = new BdvdDB();
        var select = from p in db.arma_ocorrencia
                              select p;

        var results = select.ToList<arma_ocorrencium>(); //Execute the query here

        if (logger.IsInfoEnabled)
        {
            // log sql here
        }


        if (logger.IsInfoEnabled)
        {
            logger.Info("ListarArmasOcorrencia: end");
        }

        return results;
    }
A: 

You can get the generated sql like this:

string sql = select.GetQueryText();

Make sure you are using the version 3.0.0.4 or above.

Cheers

andrecarlucci
I get this error, and yes I'm using 3.0.0.4:System.Linq.IQueryable<Bdvd.Data.arma_ocorrencium>' does not contain a definition for 'GetQueryText' and no extension method 'GetQueryText' accepting a first argument of type 'System.Linq.IQueryable<Bdvd.Data.arma_ocorrencium>' could be found (are you missing a using directive or an assembly reference?)
+1  A: 

You can use the Log property of the Provider class:

_db.Provider.Log = Console.Out;

will log your SQL statements to the console. If you want to use log4net or something similar you will have to write a small mediator class that implements TextWriter and redirects all received input to log4net.

Saintedlama