visual studio 中 class 库的 Application Insights
Application Insights for class library in visual studio
我正在尝试在 class 库项目中设置和使用 application insights。我想在 visual studio.
中调试时在离线模式下使用它
在遵循了一些指南之后,我得到了这个:
(在 Engine.cs 的构造函数中 - 我的库的 'Main' class)
_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方法中为class:
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 中调试它时,在选择 Select 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>
并在项目设置中将文件设置为"do not copy"。它只需要 exist 在启动项目中,而不是在项目输出中。因为文件没有复制到输出目录,所以 AI sdk 不会加载文件,并且使用代码中配置 TelemetryClient
的任何设置。
此外,如果您仅使用 AI 进行调试,我漂亮确定您不需要Flush
调用,我相信在调试模式下,我们查找的输出是在记录遥测时写入的,而不是在发生刷新调用时写入的。
如果上面的方法有效,当你调试时,Application Insights 工具栏按钮应该也会显示它看到的事件数量,即使调试搜索只显示最近的 250 个事件。
我正在尝试在 class 库项目中设置和使用 application insights。我想在 visual studio.
中调试时在离线模式下使用它在遵循了一些指南之后,我得到了这个:
(在 Engine.cs 的构造函数中 - 我的库的 'Main' class)
_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方法中为class:
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 中调试它时,在选择 Select 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>
并在项目设置中将文件设置为"do not copy"。它只需要 exist 在启动项目中,而不是在项目输出中。因为文件没有复制到输出目录,所以 AI sdk 不会加载文件,并且使用代码中配置 TelemetryClient
的任何设置。
此外,如果您仅使用 AI 进行调试,我漂亮确定您不需要Flush
调用,我相信在调试模式下,我们查找的输出是在记录遥测时写入的,而不是在发生刷新调用时写入的。
如果上面的方法有效,当你调试时,Application Insights 工具栏按钮应该也会显示它看到的事件数量,即使调试搜索只显示最近的 250 个事件。