MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 )。并且以很友好的方式展现在页面上。
MiniProfiler官网:http://miniprofiler.com/
MiniProfiler的一个特别有用的功能是它与数据库框架的集成。除了.NET原生的 DbConnection类,MiniProfiler还内置了对实体框架(Entity Framework)以及LINQ to SQL、RavenDb和MongoDB的支持。任何执行的Step都会包括当时查询的次数和所花费的时间。为了检测常见的错误,如N+1反模式,profiler将检测仅有参数值存在差异的多个查询。
private class Lookup<T> : ConcurrentDictionary<object, T> { /* just for brevity */ }private static readonly Lookup<DbProviderServices> _DbProviderServicesCache = new Lookup<DbProviderServices>();private static readonly object _nullKeyPlaceholder = new object();//private static readonly ConcurrentDictionary<DbProviderServices, DbProviderServices> ProviderCache = new ConcurrentDictionary<DbProviderServices, DbProviderServices>();public static void Initialize(){try{DbConfiguration.Loaded += (_, a) =>{a.ReplaceService((DbProviderServices inner, object key) => _DbProviderServicesCache.GetOrAdd(key ?? _nullKeyPlaceholder, __ => new EFProfiledDbProviderServices(inner)));};//DbConfiguration.Loaded += (EventHandler<DbConfigurationLoadedEventArgs>)((_, a) =>// a.ReplaceService((DbProviderServices inner, object key) =>// new EFProfiledDbProviderServices(inner)));//MiniProfilerEF6.ExcludeEntityFrameworkAssemblies();}catch (SqlException ex){if (ex.Message.Contains("Invalid column name 'ContextKey'"))return;throw;}}}