José F. Romaniello

Las aventuras y desventuras de un codificador.

I will show you in this post two tricks you can do to enhance and add value to your nhibernate logs.

Format Sql

This is widely know trick, you can add a configuration setting to your hibernate.cfg.xml, app.config or web.config as follow:

<property name="hibernate.format_sql" value="true" />

or you can simply do it in code:

config.DataBaseIntegration(db => db.LogFormatedSql = true)

with this trick you will get nicely formated sql in your logs files.

Logging the session identifier

All nice, we have a bunch of sql logs, but we don’t know which queries belongs to which sessions. This might be useful when you are debugging an application with multiples threads or requests.

I found this trick inside NHibernate, the easy way I found so far is to add a log4net appender like this one:

<appender name="NHibernateAppender" type="log4net.Appender.RollingFileAppender">
  <appendToFile value="true"/>
  <datePattern value="yyyyMMdd"/>
  <file value="./logs/NHibernate.log"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date Session id: %property{sessionId} - %message%newline"/>
  </layout>
  <rollingStyle value="Date"/>
</appender>

See the %property{sessionId} in the conversion pattern?

Well, in order to log something there you need to do two steps.

Add a class like this one:

public class SessionIdCapturer
{
    public override string ToString()
    {
        return SessionIdLoggingContext.SessionId.ToString();
    }
}

Add the following code in some place at the application initialization:

ThreadContext.Properties["sessionId"] = new SessionIdCapturer();

That is all! I found this code inside a nhibernate test.. it is something not very known.

After doing so, your logs will look like:

2011-05-05 18:35:59,899 Session id: 5e172068-5064-44b6-bf96-99362ca05c46 - 
    SELECT
        myFoo0_.AccountId as AccountId3_0_,
        myFoo0_.Name as Name3_0_,
        myFoo0_.Version as Version3_0_ 
    FROM
        MyFoo myFoo0_
    WHERE
        myFoo0_.AccountId=@p0;
    @p0 = 1 [Type: Int32 (0)]

Another way to have this information (and much more) is to use the nhprof tool.

| More

If you follow my tweets for sure you had read one of these:

2011-05-04_1343

2011-05-04_1343_001

2011-05-04_1345

This is one of the things I most enjoy of working for Tellago. The Tellago Technology Dojo is a webinar that we have once a week to talk about new technologies. Which is a high quality webinar from qualified developers.

Today Tellago announced a series of webinars named “Tellago Technology Updates”, from Jesús Rodriguez blog:

Today, we are really pleased to announce the Tellago Technology Updates series. This program is a periodic series of webinar that will explore in detail debate some of the emerging technologies in the software industry as well as some of our implementation experiences. These Technology Updates are targeted to both IT executives trying to adopt these new technologies as well as developers and architects trying to implement them.

You can follow the Technology Updates series at http://tellago.com/what_we_says/tellago-technology-updates

BOTTOM LINE, WHETHER YOU ARE AN IT EXECUTIVE, ARCHITECT OR DEVELOPER INTERESTED ON UNDERSTANDING SOME OF THESE TECHNOLOGIES, YOU SHOULD NOT MISS THE TELLAGO TECHNOLOGY UPDATES SERIES!!!!!

The first two webinars announced are:

  • NOSQL DATABASES FOR THE .NET DEVELOPER: WHAT’S THE FUSS ALL ABOUT?
  • I LIKE IPHONE AND ANDROID BUT I AM A .NET DEVELOPER: DEVELOPING .NET APPLICATIONS FOR IPHONE AND ANDROID
You can read more about these webinars in our brand new website!
Those webinars are completely free and open to anyone.
If you missed, you will be able to watch the recording offline.

We hope to see you there!

| More

I have created a new package that add support for writing NHibernate logs with NLog.
The package is not a dll but only one class.

In order to use this package, simply install with nuget:

2011-05-04_0838

This is an example app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <appSettings>
    <add key="nhibernate-logger" value="YourNamespace.NLogFactory, YourAssemblyName"/>
  </appSettings>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="ds" xsi:type="Console"/>
    </targets>
    <rules>
      <logger name="NHibernate.SQL" minlevel="Debug" writeTo="ds" />
    </rules>
  </nlog>
</configuration>

Remember to change “YourNamespace” and “YourAssemblyName”. That is all.
You don’t need to run Configure or some initialization code as in log4net.

There is an alternative approach here but it is a little bit more complicated.

| More