简体   繁体   English

Visual Studio中类库的Application Insights

[英]Application Insights for class library in visual studio

I am trying to set up and use application insights in a class library project. 我正在尝试在类库项目中建立和使用应用程序见解 I would like to use it in offline mode while debugging in visual studio. 我想在Visual Studio中调试时以脱机模式使用它。

After following a few guides I have this: 在遵循了一些指南之后,我有了:

(In the constructor for Engine.cs - the 'Main' class for my lib) (在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();

Then in the main method for the class: 然后在该类的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;
}

Since I wish the logging to be configured (eg Azure keys etc) in the calling code consuming the lbrary this project has no other configuration and no applicationinsights.config. 由于我希望在消耗库的调用代码中配置日志记录(例如Azure密钥等),因此该项目没有其他配置,也没有applicationinsights.config。

When I debug this in VS however, after choosing Select Application Insights Resource' -> last debug session, the 'Application Insights Search has no data. 但是,当我在VS中进行调试时,选择“选择Application Insights资源”->上次调试会话后,“ Application Insights搜索”中没有数据。

In order for VS to know that your app is using application insights and for the debugger to watch for AI data, you need to have an applicationinsights.config file (even if it is basically empty with no ikey in it) inside the project that is the startup project. 为了让VS知道您的应用程序正在使用应用程序洞察力,并让调试器监视AI数据,您需要在项目内部拥有一个applicationinsights.config文件(即使它基本上没有空,也没有ikey)。启动项目。

When the debugger starts, we look to see if the kind of debugger starting is one we recognize, and if any of the startup projects have AI config. 当调试器启动时,我们将查看调试器启动的类型是否是我们可以识别的类型, 以及是否有任何启动项目都具有AI配置。 If we don't detect AI, the AI services stop watching the debugger to prevent it from slowing things down for no reason in projects without AI. 如果我们没有检测到AI,则AI服务将停止监视调试器,以防止在没有AI的项目中无故降低调试器的速度。

So just add an ApplicationInsights.config file to the startup project that is this contents: 因此,只需将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>

And set the file to "do not copy" in the project settings. 并在项目设置中将文件设置为“请勿复制”。 it just needs to exist in the startup project, not be in the project output. 它只需要存在于启动项目中,而不是存在于项目输出中。 Because the file is not copied to the output directory, the AI sdk does not load the file, and whatever settings you used to configure the TelemetryClient in code are used. 因为该文件未复制到输出目录,所以AI sdk不会加载该文件,并且会使用您用于在代码中配置TelemetryClient任何设置。

Also, if you are only using AI for the debug time stuff, i'm pretty sure you don't need the Flush call, I believe in debug mode the output we look for is written as the telemetry is logged, not when the flush call occurs. 另外,如果您将AI用于调试时间,我确定您不需要Flush调用,我相信在调试模式下,我们寻找的输出是在记录遥测时写入的,而不是在刷新时写入的通话发生。

If the above is working, when you debug, the Application Insights toolbar button should show you the number of events it has seen go by as well, even though the debug search only shows the last 250 events. 如果上面的方法有效,则在调试时,即使调试搜索仅显示最近的250个事件,Application Insights工具栏按钮也应向您显示它所经历的事件数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Application Insights工具栏不显示事件 - Visual Studio Application Insights toolbar not showing events 如何在 .NET 库 class 中记录应用程序见解? - How to log with application insights in a .NET library class? 如何防止 Visual Studio 显示应用程序洞察提醒? - How to I prevent Visual Studio from showing application insights reminder? 如何在 Visual Studio MVC 项目中使用 application Insights REST API - How to Use application Insights REST API in Visual Studio MVC project Visual Studio 2015 Express上不提供类库和控制台应用程序项目模板 - Class Library and Console Application project templates not available on Visual Studio 2015 Express 如何调试不是主C#应用程序依赖的类库(在Visual Studio中)? - How to debug a class library that is not a dependency of the main C# application (in Visual Studio)? 如何在 Visual Studio 2019 中调试 .NET 5 WinForms 应用程序,该应用程序引用 .NET Framework 4.8 class 库 - Howto debug .NET 5 WinForms application that references .NET Framework 4.8 class library in Visual Studio 2019 将现有的类库打包为Visual Studio扩展 - Packaging existing Class Library as a Visual Studio Extension Visual Studio 2015上的类库引用 - Class library references on Visual Studio 2015 在Visual Studio在线构建中部署类库 - deploying class Library in Visual studio online build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM