I have an Asp.Net MVC 4 project in VB.Net, which use Fluent Nhibernate, NHibernate and NLog. I want to log the NHibernate queries. I want to save the query at a table in my DB, but it also can be showed in a log file. Does anyone know how I can do this?
Check out this answer on how to configure NHibernate to use NLog.
NHibernate will log all queries to the NHibernate.SQL logger.
Related
I am using nhibernate to query my db and am seeing various statements in my log files as follow -
static sql for entity
static select for entity
version select
snapshot select
followed by queries using my domain entities.
I believe these are common when building a session factory, but I just wanted to make sure. Does anyone know what these entries mean?
thanks for any help.
It's just letting you know the sql it's going to use the predictable queries such as get by id etc. They are created when setting up the session factory and then cached for optimization. Very normal.
Is there a way to tell NHibernate to leave some tables alone? I have the ASPNET Membership tables, and while I would like to be able to access them using NHibernate, I don't want SchemaExport to delete them and recreate them while recreating the other tables. (Goodness knows what may happen)
You need to use SchemaAction.None()
More information here: http://www.lostechies.com/blogs/rodpaddock/archive/2010/06/29/using-fluent-nhibernate-with-legacy-databases.aspx
In the application I'm developing, I am using NHibernate as ORM.
My app has to support SQL Server, and MS Access (I'm using the NHibernate JetDriver).
Now, the thing is that I have a certain query which I cannot achieve using HQL or the ICriteria API; so, I'm using an ISQLQuery for that one.
Now, what bothers me, is that it seems that NHibernate is parsing the native SQL query as well, and thereby changing the SQL code of that query.
It seems that the specific Driver implementation is called, and my query is parsed; and in the case of the JetDRiver, the query is being modified by NHibernate which results in a query that is unexecutable.
So, why is it that NHibernate changes my native SQL queries ?
NHibernate does some changes even in native queries in order to be able to map the entities correctly.
My suggestion... download the JetDriver source from https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.JetDriver/ and debug to see what's being broken. It might be a bug there.
I'm using log4net with NHibernate and i'm logging SQL statements generated by NHibernate.
Is there any way to instruct NHibernate to only log DML SQL statements (inserts, updates, deletes), for example something like "NHibernate.SQL" + ".DML" in log configuration?
You should just be able to add another logger node to your configuration file for NHibernate.DML That will log both.
No. Unfortunately there is no built in way for what you want.
Let's say if I encounter an error in NHibernate db update, and when such an exception is thrown, I want to log it in the log table in the db, via NHibernate again.
Now, since I can't reuse NHibernate session after an exception thrown, I will get an error when I log into the log table. How to best handle this situation?
Open a new session.
Or use a logging framework like log4net with ADO.NET appender and do it completely separately out of NHibernate environment.
IMO logging should be a cross cutting concern of your application. You should not try to reuse the same session in order to log errors. NHibernate already uses log4net. All you need is to configure an appender that will write logs to the SQL database.