servicestackormlite-servicestackminiprofiler

How to profile many connections with ServiceStack.MiniProfiler?


After registering my connections, I want to profile them. With the code below, I only profile the main connection (guepard).

    public static IDbConnectionFactory RegisterConnections(this Container self, bool enableProfiler)
    {
        var dbFactory = new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["guepard"].ConnectionString, SqlServer2008Dialect.Provider);
        self.Register<IDbConnectionFactory>(
            c =>
            {
                var cs = ConfigurationManager.ConnectionStrings;
                dbFactory.RegisterConnection("gecko-log", cs["gecko-log"].ConnectionString, SqlServerDialect.Provider);
                dbFactory.RegisterConnection("ksmpro", cs["ksmpro"].ConnectionString, SqlServer2012Dialect.Provider);
                dbFactory.RegisterConnection("gestion-stock", cs["gestion-stock"].ConnectionString, SqlServerDialect.Provider);
                dbFactory.RegisterConnection("planning", cs["planning"].ConnectionString, SqlServerDialect.Provider);
                dbFactory.RegisterConnection("febus", cs["febus"].ConnectionString, SqlServerDialect.Provider);
                if (enableProfiler)
                    dbFactory.ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current);
                return dbFactory;
            }
        );
        return dbFactory;
    }

I don't know how to profile each connection. Thank you for your time.


Solution

  • You can either register an OrmLiteConnectionFactory with the ConnectionFilter, e.g:

    dbFactory.RegisterConnection("gecko-log", 
        new OrmLiteConnectionFactory(cs["gecko-log"].ConnectionString, 
            SqlServerDialect.Provider, 
            setGlobalDialectProvider: false) {
            ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
        }
    );
    

    Or go through each NamedConnection factory after registering them to set the ConnectionFilter, e.g:

    OrmLiteConnectionFactory.NamedConnections.Values
        .Each(f => f.ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current));