public void SaveProfilingRecord(DbProfilingRecord dbProfilingRecord) { const string sqlProfiling = @"INSERT INTO [Profiling] ( [Name], [Url], [Referer], [Action], [Event], [ClientIP], [ClientAgent], [RequestID], [ActiveUser], [MachineName], [Started], [Duration], [ResponseSize], [RequestSize], [Exception], [SessionID]) VALUES ( @Name, @Url, @Referer, @Action, @Event, @ClientIP, @ClientAgent, @RequestID, @ActiveUser, @MachineName, @Started, @Duration, @ResponseSize, @RequestSize, @Exception, @SessionID) SELECT SCOPE_IDENTITY()" ; const string sqlProfilingClient = @"INSERT INTO [ProfilingClient] ( [RequestID], [ClientTotalDuration], [ClientRedirectDuration], [ClientDnsDuration], [ClientConnectionDuration], [ClientRequestDuration], [ClientResponseDuration], [ClientDomDuration], [ClientLoadDuration]) SELECT @RequestID, @ClientTotalDuration, @ClientRedirectDuration, @ClientDnsDuration, @ClientConnectionDuration, @ClientRequestDuration, @ClientResponseDuration, @ClientDomDuration, @ClientLoadDuration WHERE NOT EXISTS (SELECT 1 FROM ProfilingClient PC2 WHERE PC2.RequestID = @RequestID)" ; try { using (var conn = DataHelper.GetOpenConnection()) { // Connection created succesfully? It should be the case always, but... if (conn == null) { throw new Exception("Mini Profiler Error. Couldn't create connection to database. Suspending..."); } // Create command DbCommand cmd = conn.CreateCommand(); // Command created succesfully? if (cmd == null) { throw new Exception("Mini Profiler Error. Couldn't create command. Suspending..."); } cmd.CommandText = sqlProfiling; cmd.Parameters.Add(new SqlParameter("Name", dbProfilingRecord.Name.Truncate(64))); cmd.Parameters.Add(new SqlParameter("Url", dbProfilingRecord.Url.Truncate(256))); cmd.Parameters.Add(new SqlParameter("Referer", dbProfilingRecord.Referer.Truncate(256).ToDBNull())); cmd.Parameters.Add(new SqlParameter("Action", dbProfilingRecord.Action.Truncate(10).ToDBNull())); cmd.Parameters.Add(new SqlParameter("Event", dbProfilingRecord.Event.Truncate(128).ToDBNull())); cmd.Parameters.Add(new SqlParameter("ClientIP", dbProfilingRecord.ClientIP.Truncate(15).ToDBNull())); cmd.Parameters.Add(new SqlParameter("ClientAgent", dbProfilingRecord.ClientAgent.Truncate(128).ToDBNull())); cmd.Parameters.Add(new SqlParameter("RequestID", dbProfilingRecord.RequestID.Truncate(36).ToDBNull())); cmd.Parameters.Add(new SqlParameter("ActiveUser", dbProfilingRecord.ActiveUser.Truncate(30).ToDBNull())); cmd.Parameters.Add(new SqlParameter("MachineName", dbProfilingRecord.MachineName.Truncate(100))); cmd.Parameters.Add(new SqlParameter("Started", dbProfilingRecord.Started)); cmd.Parameters.Add(new SqlParameter("Duration", dbProfilingRecord.Duration)); cmd.Parameters.Add(new SqlParameter("ResponseSize", dbProfilingRecord.ResponseSize)); cmd.Parameters.Add(new SqlParameter("RequestSize", dbProfilingRecord.RequestSize)); cmd.Parameters.Add(new SqlParameter("Exception", dbProfilingRecord.Exception.Truncate(512).ToDBNull())); cmd.Parameters.Add(new SqlParameter("SessionID", dbProfilingRecord.SessionID.Truncate(24).ToDBNull())); // Capture Profiling ID dbProfilingRecord.ProfilingId = cmd.ExecuteScalar().ToInt(); // Save client tracking if (dbProfilingRecord.ClientRequestID != null) { cmd = conn.CreateCommand(); cmd.CommandText = sqlProfilingClient; cmd.Parameters.Add(new SqlParameter("RequestID", dbProfilingRecord.ClientRequestID)); cmd.Parameters.Add(new SqlParameter("ClientTotalDuration", dbProfilingRecord.ClientTotalDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientRedirectDuration", dbProfilingRecord.ClientRedirectDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientDnsDuration", dbProfilingRecord.ClientDnsDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientConnectionDuration", dbProfilingRecord.ClientConnectionDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientRequestDuration", dbProfilingRecord.ClientRequestDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientResponseDuration", dbProfilingRecord.ClientResponseDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientDomDuration", dbProfilingRecord.ClientDomDuration.MaxDigits(6))); cmd.Parameters.Add(new SqlParameter("ClientLoadDuration", dbProfilingRecord.ClientLoadDuration.MaxDigits(6))); cmd.ExecuteNonQuery(); } // Save timings if (dbProfilingRecord.Timings != null) { foreach (DbProfilingTimingRecord dbProfilingTimingRecord in dbProfilingRecord.Timings) { SaveTimingRecord(conn, dbProfilingRecord, dbProfilingTimingRecord); } } } } catch (Exception ex) { // Log exception Logger.Log(ex); // Error? Suspend profiling MiniProfiler.SuspendStorage(ex); } }