Visual Studio

时间:2017-01-04 13:17:28

标签: c# azure-application-insights

我正在尝试在类库项目中设置并使用application insights。我希望在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();

然后在课程的主要方法中:

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;
}

由于我希望在使用lbrary的调用代码中配置日志记录(例如Azure密钥等),因此该项目没有其他配置,也没有applicationinsights.config。

当我在VS中调试它时,选择了Select Application Insights Resource&#39; - &GT;在上次调试会话中,“应用程序洞察搜索”没有数据。

1 个答案:

答案 0 :(得分:3)

为了让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电话,I相信调试模式,我们寻找的输出是在记录遥测时写的,而不是在发生冲洗呼叫时。

如果上述工作正常,则在调试时,Application Insights工具栏按钮应显示它所看到的事件数,即使调试搜索仅显示最近250个事件。