NHibernate - How to log Named Parameterised Query with parameter values?

孤街醉人 提交于 2019-12-20 04:38:16

问题


I have a parameterised named Query like this :

Query moveOutQuery = session.createSQLQuery(moveOutQueryStr.toString())
                .addEntity(MyClass.class)
                .setParameter("assignmentStatus", Constants.CHECKED_OUT)

I want to see the actual SQL query with parameters filled in. However while debugging I only get the following query:

Select * from my_assignment WHERE assignment_status in ( :assignmentStatus )

Why isn't the assignmentStatus being substituted for its real value?


回答1:


You may log each SQL to file with following code:

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();

FileAppender fileAppender = new FileAppender();
fileAppender.Name = "NHFileAppender";
fileAppender.File = logFilePath;
fileAppender.AppendToFile = true;
fileAppender.LockingModel = new FileAppender.MinimalLock();
fileAppender.Layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss}:%m%n%n");
fileAppender.ActivateOptions();

Logger logger = hierarchy.GetLogger("NHibernate.SQL") as Logger;
logger.Additivity = false;
logger.Level = Level.Debug;
logger.AddAppender(fileAppender);

hierarchy.Configured = true;

You need to call this code once at startup of your application. Output log includes values of parameters as well.

This is implemented through Log4Net; you need to add reference. This logs both success and failed statements. You can play with FileAppender and Logger class to meet your additional requirements. This Q/A may also help.



来源:https://stackoverflow.com/questions/54182141/nhibernate-how-to-log-named-parameterised-query-with-parameter-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!