MiniProfiler integration for NHibernate, suitable for all database drivers.
Install NHibernate.MiniProfiler
Using the ProfiledDriver extension method:
var cfg = new Configuration().DataBaseIntegration(c => { c.ProfiledDriver<MySqlConnectorDriver>(); });
or by decorating the driver class:
// NHibernate
var cfg = new Configuration().DataBaseIntegration(c => { c.Driver<ProfiledDriver<MySqlConnectorDriver>>(); });
// Fluent NHibernate
MySQLConfiguration.Standard.Driver<ProfiledDriver<MySqlConnectorDriver>>();
In addition to profiling SQL statements you can also profile the Query/2nd Level Cache.
Using the ProfiledCache extension method:
var cfg = new Configuration().Cache(c => { c.ProfiledCache<CoreMemoryCacheProvider>(); });
Decorating the cache provider class:
var cfg = new Configuration().Cache(c => { c.Provider<ProfiledCacheProvider<CoreMemoryCacheProvider>>(); });
There are a couple of options available for profiling the NHibernate caches:
Config Key | Description |
---|---|
cache.profiling.includestacktracesnippet | Configures whether to include the stack trace in the profiling results. Default is false. This setting will not have any effect, if the MiniProfiler setting ExcludeStackTraceSnippetFromCustomTimings is set to true. |
cache.profiling.categoryname | Configures the category name to use in the MiniProfiler results view. Default is NHibernate Cache. |
cache.profiling.includeregionincategoryname | Configures whether to include the name of the cache region that is being profiled in the MiniProfiler results view. Default is false. |
To set these options, use the SetProperty method of the NHibernate Configuration
class e.g.:
var cfg = new Configuration();
cfg.SetProperty(CacheProfilingOptions.ConfigKeys.IncludeStackTraceSnippet, "true");
cfg.SetProperty(CacheProfilingOptions.ConfigKeys.IncludeRegionInCategoryName, "true");
System.InvalidCastException: Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbConnection' to type 'Your data provider's DbConnection'.
This may happen under ASP.NET on .NET Framework when using async/await, because MiniProfiler.Current
is null after an async transition. The causes ProfiledDriver
to return the implementation of DbCommand
from the database provider instead of returning an instance of ProfiledDbCommand
.
To resolve this, configure MiniProfiler's ProfilerProvider
setting as follows:
MiniProfiler.Configure(new MiniProfilerOptions
{
ProfilerProvider = new AspNetRequestProvider(true)
});
This software is distributed under the terms of the Free Software Foundation Lesser GNU Public License (LGPL), version 2.1 (see LICENSE.txt).