private async Task InsertXEventAsync(XEventItem eItem) { SqlCommand QICommand; QICommand = new SqlCommand(); QICommand.CommandType = CommandType.StoredProcedure; QICommand.CommandText = "dbo.InsertXETraceEvent"; QICommand.Parameters.Add("@event_name", SqlDbType.VarChar, 75).Value = eItem.event_name; QICommand.Parameters.Add("@server_name", SqlDbType.VarChar, 30).Value = eItem.server_name; QICommand.Parameters.Add("@event_timestamp", SqlDbType.DateTime2, 8).Value = eItem.event_timestamp; QICommand.Parameters.Add("@session_id", SqlDbType.VarChar, 200).Value = eItem.session_id; QICommand.Parameters.Add("@transaction_id", SqlDbType.VarChar, 200).Value = eItem.transaction_id; QICommand.Parameters.Add("@database_name", SqlDbType.VarChar, 255).Value = eItem.database_name; QICommand.Parameters.Add("@nt_username", SqlDbType.VarChar, 40).Value = eItem.nt_username; QICommand.Parameters.Add("@object_name", SqlDbType.VarChar, 255).Value = eItem.object_name; QICommand.Parameters.Add("@duration", SqlDbType.BigInt).Value = eItem.duration; lock (_statementTextLocker) { QICommand.Parameters.Add("@statement", SqlDbType.NVarChar, -1).Value = eItem.statement.ToString(); } lock (_xmlTextLocker) { QICommand.Parameters.Add("@xml_report", SqlDbType.NVarChar, -1).Value = eItem.xml_report.ToString(); } QICommand.Parameters.Add("@client_hostname", SqlDbType.VarChar, 30).Value = eItem.client_hostname; QICommand.Parameters.Add("@application_name", SqlDbType.VarChar, 255).Value = eItem.application_name; QICommand.Parameters.Add("@cpu_time", SqlDbType.BigInt).Value = eItem.cpu_time; QICommand.Parameters.Add("@physical_reads", SqlDbType.BigInt).Value = eItem.physical_reads; QICommand.Parameters.Add("@logical_reads", SqlDbType.BigInt).Value = eItem.logical_reads; QICommand.Parameters.Add("@writes", SqlDbType.BigInt).Value = eItem.writes; QICommand.Parameters.Add("@row_count", SqlDbType.BigInt).Value = eItem.row_count; QICommand.Parameters.Add("@causality_guid", SqlDbType.VarChar, 36).Value = eItem.causality_guid; QICommand.Parameters.Add("@causality_seq", SqlDbType.BigInt).Value = eItem.causality_seq; QICommand.Parameters.Add("@nest_level", SqlDbType.BigInt).Value = eItem.nest_level; QICommand.Parameters.Add("@wait_type", SqlDbType.NVarChar, 120).Value = eItem.wait_type; QICommand.Parameters.Add("@wait_resource", SqlDbType.NVarChar, 3072).Value = eItem.wait_resource; QICommand.Parameters.Add("@resource_owner_type", SqlDbType.NVarChar, 60).Value = eItem.resource_owner_type; QICommand.Parameters.Add("@lock_mode", SqlDbType.VarChar, 30).Value = eItem.lock_mode; foreach (SqlParameter sp in QICommand.Parameters) { if (sp.Value == "") { sp.Value = System.DBNull.Value; } } using (SqlConnection insConn = new SqlConnection("Server=" + _MonitoringServer + ";Database=XEventTrace;Application Name=XEventInsert-" + _ServerName + ";Trusted_Connection=True;Min Pool Size=10;Max Pool Size=500;")) { using (QICommand) { QICommand.Connection = insConn; await insConn.OpenAsync().ConfigureAwait(true); await QICommand.ExecuteNonQueryAsync().ContinueWith(_ => insConn.Close()); } } }
private async void QueueEventItemForInsert(XEventItem ei, EventArgs ea) { try { await InsertXEventAsync(ei).ConfigureAwait(true); }catch (Exception ex) { Console.WriteLine("Error on async insert" + ex.ToString()); } }