【问题标题】:Application Insights for class library in visual studioVisual Studio 中类库的 Application Insights
【发布时间】:2017-01-04 13:17:27
【问题描述】:

我正在尝试在类库项目中设置和使用application insights。我想在 Visual Studio 中调试时在离线模式下使用它。

遵循一些指南后,我有这个:

(在 Engine.cs 的构造函数中 - 我的库的“主”类)

_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 搜索没有数据”。

【问题讨论】:

  • 对不起,如果这听起来不对,但如果我错了,请纠正我。我认为创建应用程序洞察力是为了确定系统在负载下的运行情况(即有多少人同时访问系统的某些部分)。使用 Application Insights 会让您知道只有您在访问它。使用这个系统做一个仅限本地的图书馆时需要什么数据?也许改用内置的配置文件系统?
  • 这是完全有效的 appinsights 用法。它还处理使用情况(自定义事件)、异常等。

标签: c# azure-application-insights


【解决方案1】:

为了让 VS 知道您的应用正在使用应用程序洞察并让调试器监视 AI 数据,您需要在项目即启动项目。

当调试器启动时,我们会查看启动的调试器类型是否是我们识别的,以及启动项目中是否有任何 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 电话,我相信调试模式下,我们查找的输出是在记录遥测时写入的,而不是在刷新调用发生时写入。

如果上述方法有效,则在您进行调试时,Application Insights 工具栏按钮也应该会显示它所看到的事件数,即使调试搜索仅显示最近的 250 个事件。

【讨论】:

  • 完美运行!谢谢你的帮助。有点奇怪,因为 AI 文档声明不需要配置文件。
  • 如果您在代码中完成了配置,则在运行时AI 收集+发送遥测数据不需要该配置文件,但工具中的某些功能需要该配置文件。
猜你喜欢
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
相关资源
最近更新 更多