public void LogCommandExecution(DbCommand command, DataService dataService, TimeSpan executionTime) { if (isRunning) { var blockingCollection = this.blockingCollection; if (blockingCollection == null || blockingCollection.IsAddingCompleted) { return; } var record = new ProfilerRecord { CommandText = command.CommandText, CommandType = command.CommandType, Context = dataService?.ApplicationContextGetter(), ExecutionDate = DateTime.UtcNow, ExecutionDelay = executionTime, UserId = dataService.CurrentUserId, Parameters = command.Parameters.Cast <DbParameter>().ToDictionary(x => x.ParameterName, x => x.Size < 0 || x.Size > 4000 ? "#value too large#" : x.Value) }; try { blockingCollection.Add(record); } catch (Exception ex) { logger.LogError(ex, "Failed to add record to blocking collection"); } } }
private async Task ProcessQueue() { bool stopRequested = false; bool error = false; using (var bulkInsert = new BulkInsertOperation(this.database, this.store)) using (var session = this.store.OpenAsyncSession(this.database)) { session.Advanced.MaxNumberOfRequestsPerSession = int.MaxValue; while (true) { ProfilerRecord record = null; try { record = blockingCollection.Take(); } catch (ObjectDisposedException) { stopRequested = true; } catch (InvalidOperationException) { stopRequested = true; } catch (OperationCanceledException) { stopRequested = true; } catch (Exception ex) { logger.LogError(ex, "Failed to read record from blocking collection"); error = true; try { this.StopProfilingImpl(); } catch { } } if (stopRequested || error) { try { this.blockingCollection.Dispose(); } catch { } this.blockingCollection = null; return; } var sqlStatementId = "SqlStatements/" + GetHash(record.CommandText); var normalizedCommandText = listOfValuesRegex.Replace(record.CommandText, "( #ListOfValues# )"); var normalizedSqlStatementId = "NormalizedSqlStatements/" + GetHash(normalizedCommandText); if (normalizedSqlStatementIds.Add(normalizedSqlStatementId)) { if (!await session.Advanced.ExistsAsync(normalizedSqlStatementId)) { var normalizedSqlStatement = new NormalizedSqlStatement { Id = normalizedSqlStatementId, Sql = normalizedCommandText }; await bulkInsert.StoreAsync(normalizedSqlStatement, normalizedSqlStatementId); } } if (sqlStatementIds.Add(sqlStatementId)) { if (!await session.Advanced.ExistsAsync(sqlStatementId)) { var sqlStatement = new SqlStatement { Id = sqlStatementId, NormalizedSqlStatementId = normalizedSqlStatementId, Sql = record.CommandText }; await bulkInsert.StoreAsync(sqlStatement, sqlStatementId); } } var sqlStatementExecution = new SqlStatementExecution { CommandType = record.CommandType, Context = record.Context, }; var metadata = new Dictionary <string, object> { [Constants.Documents.Metadata.Expires] = DateTime.UtcNow.Add(this.expiration) }; await bulkInsert.StoreAsync(sqlStatementExecution, sqlStatementExecution.Id, new MetadataAsDictionary(metadata)); } } }