瀏覽代碼

DiagnosticObserver does not use null connection (#845)

* DiagnosticObserver does not use null connection

* Make TryGetSqlConnection static.

Co-authored-by: Dave Smith <dave.smith@ventivtech.com>
master
David Smith 3 年之前
committed by GitHub
父節點
當前提交
83682ac0ab
沒有發現已知的金鑰在資料庫的簽署中 GPG Key ID: 4AEE18F83AFDEB23
共有 1 個文件被更改,包括 10 次插入6 次删除
  1. +10
    -6
      src/DotNetCore.CAP.SqlServer/Diagnostics/DiagnosticObserver.cs

+ 10
- 6
src/DotNetCore.CAP.SqlServer/Diagnostics/DiagnosticObserver.cs 查看文件

@@ -13,9 +13,7 @@ namespace DotNetCore.CAP.SqlServer.Diagnostics
{
internal class DiagnosticObserver : IObserver<KeyValuePair<string, object>>
{
public const string SqlAfterCommitTransaction = "System.Data.SqlClient.WriteTransactionCommitAfter";
public const string SqlAfterCommitTransactionMicrosoft = "Microsoft.Data.SqlClient.WriteTransactionCommitAfter";
public const string SqlErrorCommitTransaction = "System.Data.SqlClient.WriteTransactionCommitError";
public const string SqlErrorCommitTransactionMicrosoft = "Microsoft.Data.SqlClient.WriteTransactionCommitError";

private readonly ConcurrentDictionary<Guid, List<MediumMessage>> _bufferList;
@@ -38,9 +36,9 @@ namespace DotNetCore.CAP.SqlServer.Diagnostics

public void OnNext(KeyValuePair<string, object> evt)
{
if (evt.Key == SqlAfterCommitTransaction || evt.Key == SqlAfterCommitTransactionMicrosoft)
if (evt.Key == SqlAfterCommitTransactionMicrosoft)
{
var sqlConnection = (SqlConnection)GetProperty(evt.Value, "Connection");
if (!TryGetSqlConnection(evt, out SqlConnection sqlConnection)) return;
var transactionKey = sqlConnection.ClientConnectionId;
if (_bufferList.TryRemove(transactionKey, out var msgList))
foreach (var message in msgList)
@@ -48,15 +46,21 @@ namespace DotNetCore.CAP.SqlServer.Diagnostics
_dispatcher.EnqueueToPublish(message);
}
}
else if (evt.Key == SqlErrorCommitTransaction || evt.Key == SqlErrorCommitTransactionMicrosoft)
else if (evt.Key == SqlErrorCommitTransactionMicrosoft)
{
var sqlConnection = (SqlConnection)GetProperty(evt.Value, "Connection");
if (!TryGetSqlConnection(evt, out SqlConnection sqlConnection)) return;
var transactionKey = sqlConnection.ClientConnectionId;

_bufferList.TryRemove(transactionKey, out _);
}
}

private static bool TryGetSqlConnection(KeyValuePair<string, object> evt, out SqlConnection sqlConnection)
{
sqlConnection = GetProperty(evt.Value, "Connection") as SqlConnection;
return sqlConnection != null;
}

private static object GetProperty(object _this, string propertyName)
{
return _this.GetType().GetTypeInfo().GetDeclaredProperty(propertyName)?.GetValue(_this);


Loading…
取消
儲存