NHibernate - logging - nhibernate

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.

Related

How to log NHibernate query?

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.

static sql for entity in nhibernate log file

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.

How to use SQL Cache Dependency Wtih EF DbContext?

I found this article on using Sql Cache Dependency with Linq2SQL.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=263
is it possible to do this same thing in Entity Framework with DbContext?
I have a multi-database app so each DbContext Instance Connection is different depending on the user.
But I feel like utilizing this technology would be the best way to invalidate the cache.
any help is appreciated.
Chase
Take a look at this, will give you a starting point: http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx, I tried it and it works fine but I have chosen to use a more custom and light weight approach.
Basically I retrieve the TraceString which is nothing but the raw SQL from my IQueriable<T> and create a standard SqlCacheDependency cache entry.
Your DbContext should live only for single unit of work. In your case for single request processing so there is no reason to involve any database dependency in EF.

Dump Hibernate activity to sql script file

I'm trying to log hibernate activity (only dml operations) to an sql script file.
My goal is to have a way to reconstruct the database from a given starting point to the current state by executing the generated script.
I can get the sql queries from log4j logs but they have more information than the raw sql queries and i would need to parse them and extract only the helpful statements.
So i'm looking for a programatic way, maybe by listening the persist/merge/delete operations and accessing the hibernate-generated sql statements.
I don't like to reinvent the wheel so, if anybody know a way for doing this i would appreciate it very much.
Thanks in advance
Generally the best way to do this is to just turn on logging on your SQL server. All the major RDBMSes support logging all the SQL statements that they run. This has the added advantage of catching things that happened outside of Hibernate.
You could also try to use NHProf which will intercept/record hibernate traffic to the database and dump it into an XML file. You might have to parse the file by hand, but all the information will be there.
You could also hook at the JDBC level directly and record the JDBC statements that are performed.
P6Spy is a great tool to inspect what's going on. It can log the queries, though I don't know if you can replay them as is.
I'm sure there are other such tool (or at worse you could try subclass the DataSource, Connection and PreparedStatement implementation of your choice to do that yourself).

How to Insert into NHibernate DB after NHibernate Session

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.