簡體   English   中英

Visual Studio中類庫的Application Insights

[英]Application Insights for class library in visual studio

我正在嘗試在類庫項目中建立和使用應用程序見解 我想在Visual Studio中調試時以脫機模式使用它。

在遵循了一些指南之后,我有了:

(在Engine.cs的構造函數中-我的lib的“ Main”類)

_telemetryClient = new TelemetryClient();

// Set session data:
_telemetryClient.Context.User.Id = Environment.UserName;
_telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
_telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();

然后在該類的main方法中:

var metrics = new Dictionary<string, double>();
var properties = new Dictionary<string, string>();
try
{
    // Do stuff and track metrics code...

    telemetryClient?.TrackEvent("Some Event", properties, metrics);
    _telemetryClient?.Flush();
    System.Threading.Thread.Sleep(1000);
}
catch (Exception exception)
{
    _telemetryClient?.TrackException(exception, properties, metrics);
    _telemetryClient?.Flush();
    throw;
}

由於我希望在消耗庫的調用代碼中配置日志記錄(例如Azure密鑰等),因此該項目沒有其他配置,也沒有applicationinsights.config。

但是,當我在VS中進行調試時,選擇“選擇Application Insights資源”->上次調試會話后,“ Application Insights搜索”中沒有數據。

為了讓VS知道您的應用程序正在使用應用程序洞察力,並讓調試器監視AI數據,您需要在項目內部擁有一個applicationinsights.config文件(即使它基本上沒有空,也沒有ikey)。啟動項目。

當調試器啟動時,我們將查看調試器啟動的類型是否是我們可以識別的類型, 以及是否有任何啟動項目都具有AI配置。 如果我們沒有檢測到AI,則AI服務將停止監視調試器,以防止在沒有AI的項目中無故降低調試器的速度。

因此,只需將ApplicationInsights.config文件添加到以下內容的啟動項目中:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
    <!-- this file should NOT be set to copy output, it is here to allow the AI tools in Visual Studio to watch this project when debugging -->
</ApplicationInsights>

並在項目設置中將文件設置為“請勿復制”。 它只需要存在於啟動項目中,而不是存在於項目輸出中。 因為該文件未復制到輸出目錄,所以AI sdk不會加載該文件,並且會使用您用於在代碼中配置TelemetryClient任何設置。

另外,如果您將AI用於調試時間,我確定您不需要Flush調用,我相信在調試模式下,我們尋找的輸出是在記錄遙測時寫入的,而不是在刷新時寫入的通話發生。

如果上面的方法有效,則在調試時,即使調試搜索僅顯示最近的250個事件,Application Insights工具欄按鈕也應向您顯示它所經歷的事件數。

暫無
暫無

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

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