Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:32:09+00:00 2026-06-05T23:32:09+00:00

Configuring NHibernate to display executed SQL does what it’s supposed to, but whenever a

  • 0

Configuring NHibernate to display executed SQL does what it’s supposed to, but whenever a SQL string needs to be copy-pasted into SQL Server Management Studio, we have to rearrange it dramatically in order to be compatible.

Before I dive into developing my own application that parses and rearranges this into a more ManagementStudio-friendly SQL, I’d like to reassert that this hasn’t been done before – I’d hate to spend time on this and find out later.

Is there a cheap and practicable way of converting the NH-generated prepared statement into something that’s executable straight away?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:32:10+00:00Added an answer on June 5, 2026 at 11:32 pm

    I know you can do this with nhibernate profiler but this is not a free tool. I would also be interested in a free alternative to doing this.

    http://nhprof.com/

    Edit

    Looks like there is a custom appender out there for log4net that will format it such that you can actually run the sql NHibernate spits out. I saw it in the blog listed below:

    http://gedgei.wordpress.com/2011/09/03/logging-nhibernate-queries-with-parameters/

    Below is the code I have taken from the above blog and modified to work with Guids:

    /// <summary>
    /// This log4net appender is used for outputting NHibernate sql statements in a sql management studio friendly format.
    /// This means you should be able to copy the sql output from this appender and run it directly.  Normally in the NHibernate
    /// output there is parameterized sql that must be manually edited to run it.
    /// </summary>
    public class NHibernateSqlAppender : ForwardingAppender
    {
        private const string GuidRegex = @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b";
    
        protected override void Append(LoggingEvent loggingEvent)
        {
            var loggingEventData = loggingEvent.GetLoggingEventData();
    
            if (loggingEventData.Message.Contains("@p"))
            {
                StringBuilder messageBuilder = new StringBuilder();
    
                string message = loggingEventData.Message;
                var queries = Regex.Split(message, @"command\s\d+:");
    
                foreach (var query in queries)
                    messageBuilder.Append(ReplaceQueryParametersWithValues(query));
    
                loggingEventData.Message = messageBuilder.ToString();
            }
    
            base.Append(new LoggingEvent(loggingEventData));
        }
    
        public static string ReplaceQueryParametersWithValues(string query)
        {
            string returnQuery = Regex.Replace(query, @"@p\d+(?=[,);\s])(?!\s*=)", match =>
            {
                Regex parameterValueRegex = new Regex(string.Format(@".*{0}\s*=\s*(.*?)\s*[\[].*", match));
                return parameterValueRegex.Match(query).Groups[1].ToString();
            });
    
            //Place single quotes around all Guids in the sql string
            returnQuery = Regex.Replace(returnQuery, GuidRegex, "'$0'", RegexOptions.IgnoreCase);
    
            int parameterListIndex = returnQuery.LastIndexOf("@p0");
    
            if (parameterListIndex != -1)
            {
                //Truncate the paramter list off the end since we are substituting the actual values in the regular expression above
                //The -1 also cuts off the semicolon at the end
                return returnQuery.Substring(0, parameterListIndex).Trim();
            }
    
            return returnQuery.Trim();
        }
    }
    

    Here is how you would send this output to the console:

    <appender name="NHibernateSqlAppender" type="NHibernatePlayground.Custom.NHibernateSqlAppender, NHibernatePlayground">
        <appender-ref ref="console" />
    </appender>
    
    <root>
        <appender-ref ref="NHibernateSqlAppender" />
    </root>
    

    NOTE:

    It appears this causes some fairly significant performance issues in a production system. I haven’t found a better way to do this yet but for anyone using this beware of these performance issues

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to configure my NHibernate.Validator instance as described in this post Configuring
I am having the following error when configuring mvc3 and Nhibernate. Can anyone guide
I need help with configuring NHibernate. In my solution has several projects: first contains
I'm configuring JPA to work with SQLite db, with hibernate as the provider. I
When configuring nginx with a site that has ssl, the examples I find online
When configuring a Hadoop Cluster whats the scientific method to set the number of
im configuring my EC2 instance (amazon web services) on an ubuntu server, to test
I'm working on configuring a RollingFileAppender in log4net programmatically. I have something like this
I'm configuring a web site using PowerShell and I want to set the default
I am configuring an ADO.NET DataService. public static void InitializeService(IDataServiceConfiguration config) { // config.UseVerboseErrors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.