簡體   English   中英

如何在嘗試為偵聽器設置Azure接收器時跟蹤異常源

[英]How can I track the source of exception on trying to set Azure sink for a listener

我正在嘗試使用Semantic Logging Application Block將日志存儲到Azure Table Storage 設定:

ObservableEventListener listener1 = new ObservableEventListener();
var conString =
    $"DefaultEndpointsProtocol={CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint.Scheme};" +
    $"AccountName={CloudStorageAccount.DevelopmentStorageAccount.Credentials.AccountName};" +
    $"AccountKey={Convert.ToBase64String(CloudStorageAccount.DevelopmentStorageAccount.Credentials.ExportKey())}";

listener1.LogToWindowsAzureTable( // <---- EXCEPTION HERE
        instanceName: "instName",
        connectionString: conString);

我收到一個奇怪的異常:

引發的異常:Microsoft.Practices.EnterpriseLibrary.SemanticLogging.WindowsAzure.dll中的“ System.MissingMethodException”

附加信息:找不到方法:'無效Microsoft.WindowsAzure.Storage.Table.CloudTableClient.set_RetryPolicy(Microsoft.WindowsAzure.Storage.RetryPolicies.IRetryPolicy)'。

我的真實帳戶也有同樣的問題。 軟件包版本(全部來自NuGet):

  • EnterpriseLibrary.SemanticLogging — 2.0.1406.1
  • EnterpriseLibrary.SemanticLogging.WindowsAzure — 2.0.1406.1
  • WindowsAzure.Storage — 7.0.0

如何跟蹤異常的來源? Google對未找到的方法一言不發。 測試你的機器上的一個項目是在這里

收到此錯誤的原因是因為SLAB依賴於存儲客戶端庫3.0.2.0( source ),並且在客戶端(例如CloudTableClient )上設置Retry Policies在版本4.0.0.0( source )中已棄用,在某些更高版本中已刪除(不確定是哪一個)。

由於您使用的是存儲客戶端庫的7​​.x版本,因此不存在在CloudTableClient上設置RetryPolicy的方法,因此會出現此錯誤。

就像Guarav所說的那樣,SLAB是針對非常老的Microsoft.WindowsAzure.Storage版本構建的。 問題是此行 ,使用client.RetryPolicy而不是client.DefaultRequestOptions.RetryPolicy 我嘗試更新NuGet軟件包並進行更改,但是它似乎破壞了一些測試,並且修復它們似乎並不容易。 看起來還不存在4.6支持: https : //github.com/mspnp/semantic-logging/issues/64

這里有一個缺陷: https : //github.com/mspnp/semantic-logging/issues/81 ,但是我懷疑很快就會發生任何事情(如果有的話)。 我可能最終會寫一些簡單的接收器,將日志重定向到Trace Listener以便Azure處理(包括Table Storage upload )。

編輯下面的代碼(尚未測試):

public class SystemDiagnosticsTraceSink : IObserver<EventEntry>
{
    public void OnNext(EventEntry entry)
    {
        if (entry == null) return;
        using (var writer = new StringWriter())
        {
            new EventTextFormatter().WriteEvent(entry, writer);
            var eventText = writer.ToString();
            switch (entry.Schema.Level)
            {
                case EventLevel.LogAlways:
                case EventLevel.Critical:
                case EventLevel.Error:
                    Trace.TraceError(eventText);
                    return;
                case EventLevel.Warning:
                    Trace.TraceWarning(eventText);
                    return;
                case EventLevel.Informational:
                case EventLevel.Verbose:
                    Trace.TraceInformation(eventText);
                    return;
                default:
                    Trace.TraceError("Unknown event level: " + entry.Schema.Level);
                    Trace.TraceInformation(eventText);
                    return;
            }
        }
    }
    public void OnError(Exception error)
    {} //you might want to do something here
    public void OnCompleted()
    {} //nothing to do
}

和擴展類:

public static class SystemDiagnosticsTraceSinkExtensions
{
    public static SinkSubscription<SystemDiagnosticsTraceSink> LogToSystemDiagnosticsTrace
      (this IObservable<EventEntry> eventStream)
    {
        if (eventStream == null) throw new ArgumentNullException(nameof(eventStream));

        var sink = new SystemDiagnosticsTraceSink();
        return new SinkSubscription<SystemDiagnosticsTraceSink>(
                       eventStream.Subscribe(sink), sink);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM