Exemple #1
0
        public static IDbConnection SimpleDbConnection()
        {
            var conn       = new SQLiteConnection("Data Source=" + DbFile);
            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(conn, dbProfiler));
        }
Exemple #2
0
        public IDbConnection GetConnection()
        {
            var dbConnection = new SqlConnection(_basketballLeagueConnectionString);

            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(dbConnection, dbProfiler));
        }
Exemple #3
0
        /// <summary>
        /// Get GetConnection
        /// </summary>
        /// <param name="dbName">Db Name</param>
        /// <returns></returns>
        private IDbConnection GetConnection(string dbName)
        {
            var connectionString = this._connectionStringHelper.GetConnectionString(dbName);
            var connection       = new SqlConnection(connectionString);

            //使用CoreProfiller的connection,才能紀錄T-SQ與傳入參數
            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(connection, dbProfiler));
        }
        public void TestDbProfiler_ExecuteDbCommand_InvalidExecute()
        {
            var mockStorage = new Mock <IProfilingStorage>();
            var profiler    = new Profiler("test", mockStorage.Object, null);
            var mockCommand = new Mock <IDbCommand>();
            var target      = new DbProfiler(profiler) as IDbProfiler;

            // execute empty execute should not throw exception
            target.ExecuteDbCommand(DbExecuteType.Reader, mockCommand.Object, null, null);
        }
Exemple #5
0
        /// <summary>
        /// Gets the connection.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="profiler">The profiler.</param>
        /// <returns>IDbConnection.</returns>
        private IDbConnection GetConnection(string connectionString, IProfiler profiler = null)
        {
            var conn = new SqlConnection(connectionString);

            if (profiler == null)
            {
                return(conn);
            }
            var dbProfiler = new DbProfiler(profiler);

            return(new ProfiledDbConnection(conn, dbProfiler));
        }
        private DbConnection GetConnection()
        {
            var conn = new SqlConnection(@"Server=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SimpleDemoDB.mdf;Database=SimpleDemoDB;Trusted_Connection=Yes;");

            if (ProfilingSession.Current == null)
            {
                return(conn);
            }

            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(conn, dbProfiler));
        }
        /// <summary>
        /// 取得連線
        /// </summary>
        /// <returns></returns>
        public IDbConnection GetConnection()
        {
            var connection = new SqlConnection(this._connectionString);

            if (ProfilingSession.Current == null)
            {
                return(connection);
            }

            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(connection, dbProfiler));
        }
        public void TestDbProfiler_ExecuteDbCommand_InvalidCommand()
        {
            var mockStorage = new Mock <IProfilingStorage>();
            var profiler    = new Profiler("test", mockStorage.Object, null);
            var target      = new DbProfiler(profiler) as IDbProfiler;

            var executeCalled = false;

            target.ExecuteDbCommand(DbExecuteType.Reader, null, () =>
            {
                executeCalled = true;
                return(null);
            }, null);

            Assert.IsTrue(executeCalled);
        }
Exemple #9
0
 public void Loaded(DbConfigurationLoadedEventArgs loadedEventArgs,
                    DbConfigurationInterceptionContext interceptionContext)
 {
     var logger = new ExceptionLogger();
     _baseInfoQueue = new InfoQueue<BaseInfo>();
     var profiler = new DbProfiler(_baseInfoQueue)
     {
         AssembliesToExclude = new SortedSet<string>
         {
             typeof(DbProfiler).Assembly.GetName().Name,
             typeof(DatabaseLogger).Assembly.GetName().Name
         }
     };
     DbInterception.Add(new DatabaseInterceptor(profiler));
     _commandsTransmitter = new CommandsTransmitter(
         _baseInfoQueue, logger, new SimpleHttp(), _serverUri, LoggerPath.GetLogFileFullPath(_logFilePath));
     _commandsTransmitter.Start();
 }
Exemple #10
0
 private DatabaseLogger()
 {
     AppDomain.MonitoringIsEnabled = true;
     var logger = new ExceptionLogger();
     var baseInfoQueue = new InfoQueue<BaseInfo>();
     Profiler = new DbProfiler(baseInfoQueue)
     {
         AssembliesToExclude = new SortedSet<string>
         {
             typeof(DbProfiler).Assembly.GetName().Name,
             typeof(DatabaseLogger).Assembly.GetName().Name
         }
     };
     var serverUri = new Uri(ConfigSetGet.GetConfigData("DNTProfilerServerUri"));
     var logFilePath = ConfigSetGet.GetConfigData("DNTProfilerLogFilePath");
     var commandsTransmitter = new CommandsTransmitter(
         baseInfoQueue, logger, new SimpleHttp(), serverUri, LoggerPath.GetLogFileFullPath(logFilePath));
     commandsTransmitter.Start();
 }
        /// <summary>
        /// Get GetConnection
        /// </summary>
        /// <param name="dbName">Db Name</param>
        /// <returns></returns>
        private IDbConnection GetConnection(string dbName, bool isNanoProflerConn = true)
        {
            var connectionString = this._connectionStringHelper.GetConnectionString(dbName);

            var connection = new SqlConnection(connectionString);

            //如果web.config的nanoprofiler circularBufferSize="0",CircularBuffer就為null
            //表示不保留NanoProfiler的紀錄,可以測試機circularBufferSize有數值,正式就為0
            if (ProfilingSession.CircularBuffer == null || isNanoProflerConn == false)
            {
                //一般的connection
                return(connection);
            }

            //NanoProfiller的connection (可以監控到TSQL與傳入的參數)
            var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler);

            return(new ProfiledDbConnection(connection, dbProfiler));
        }
Exemple #12
0
        public void Loaded(DbConfigurationLoadedEventArgs loadedEventArgs,
                           DbConfigurationInterceptionContext interceptionContext)
        {
            var logger = new ExceptionLogger();

            _baseInfoQueue = new InfoQueue <BaseInfo>();
            var profiler = new DbProfiler(_baseInfoQueue)
            {
                AssembliesToExclude = new SortedSet <string>
                {
                    typeof(DbProfiler).Assembly.GetName().Name,
                    typeof(DatabaseLogger).Assembly.GetName().Name
                }
            };

            DbInterception.Add(new DatabaseInterceptor(profiler));
            _commandsTransmitter = new CommandsTransmitter(
                _baseInfoQueue, logger, new SimpleHttp(), _serverUri, LoggerPath.GetLogFileFullPath(_logFilePath));
            _commandsTransmitter.Start();
        }
Exemple #13
0
        private DatabaseLogger()
        {
            AppDomain.MonitoringIsEnabled = true;
            var logger        = new ExceptionLogger();
            var baseInfoQueue = new InfoQueue <BaseInfo>();

            Profiler = new DbProfiler(baseInfoQueue)
            {
                AssembliesToExclude = new SortedSet <string>
                {
                    typeof(DbProfiler).Assembly.GetName().Name,
                    typeof(DatabaseLogger).Assembly.GetName().Name
                }
            };
            var serverUri           = new Uri(ConfigSetGet.GetConfigData("DNTProfilerServerUri"));
            var logFilePath         = ConfigSetGet.GetConfigData("DNTProfilerLogFilePath");
            var commandsTransmitter = new CommandsTransmitter(
                baseInfoQueue, logger, new SimpleHttp(), serverUri, LoggerPath.GetLogFileFullPath(logFilePath));

            commandsTransmitter.Start();
        }
        public void TestDbProfiler()
        {
            var mockStorage = new Mock <IProfilingStorage>();
            var profiler    = new Profiler("test", mockStorage.Object, null);
            var target      = new DbProfiler(profiler) as IDbProfiler;

            var stepId = Guid.NewGuid();

            ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId = stepId;
            var executeCalled = false;
            var mockCommand   = new Mock <IDbCommand>();

            mockCommand.Setup(cmd => cmd.CommandText).Returns("test");
            var mockReader = new Mock <IDataReader>();

            // test execute reader
            target.ExecuteDbCommand(DbExecuteType.Reader, mockCommand.Object, () =>
            {
                executeCalled = true;
                return(mockReader.Object);
            }, new TagCollection(new[] { "tag1" }));
            Assert.IsTrue(executeCalled);
            Assert.AreEqual(0, profiler.GetTimingSession().Timings.Count(t => t.Type == "db"));
            target.DataReaderFinished(new ProfiledDbDataReader(mockReader.Object, target));
            Assert.AreEqual(1, profiler.GetTimingSession().Timings.Count(t => t.Type == "db"));
            Assert.AreEqual("tag1", profiler.GetTimingSession().Timings.Last(t => t.Type == "db").Tags.First());

            // test execute nonquery
            executeCalled = false;
            target.ExecuteDbCommand(DbExecuteType.NonQuery, mockCommand.Object, () =>
            {
                executeCalled = true;
                return(null);
            }, null);
            Assert.IsTrue(executeCalled);
            Assert.AreEqual(2, profiler.GetTimingSession().Timings.Count(t => t.Type == "db"));

            // test DataReaderFinished with invalid reader, it should not throw exception
            target.DataReaderFinished(null);
        }
        public DataSet LoadActiveDemoDataWithDataAdapter()
        {
            using (ProfilingSession.Current.Step("Data.LoadActiveDemoDataWithDataAdapter"))
            {
                using (var conn = new SqlConnection(@"Server=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SimpleDemoDB.mdf;Database=SimpleDemoDB;Trusted_Connection=Yes;"))
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "select Id, Name from [Table] where IsActive = @IsActive";
                        cmd.Parameters.Add(new SqlParameter("@IsActive", 1));

                        var dataAdapter = new SqlDataAdapter(cmd);
                        var ds          = new DataSet("SimpleDemoDB");

                        // the simplest way to enable DB profiling is to hook the DbConnection like in the method above.
                        // But if for any reason, you could not hook profiling at the DbConnection,
                        // you could also hook it inline at DataAdapter, DataReader or DbCommand.
                        if (ProfilingSession.Current != null)
                        {
                            var dbProfiler          = new DbProfiler(ProfilingSession.Current.Profiler);
                            var profiledDataAdapter = new ProfiledDbDataAdapter(dataAdapter, dbProfiler);
                            profiledDataAdapter.Fill(ds);
                        }
                        else
                        {
                            dataAdapter.Fill(ds);
                        }
                    }
                }
            }

            return(null);
        }