在调试中禁用应用程序洞察

问题描述 投票:83回答:12

如何在使用调试配置时自动禁用应用程序洞察并仅在发布时启用它? 是否可以在不创建另一个仅用于调试的检测键的情况下执行此操作? 我将trackevent语句分散在整个代码中,将它们封装在调试预处理器中并不是一个理想的解决方案。

我目前的解决方案是将Build Action文件的ApplicationInsights.config设置为None,以便它不会复制到项目的输出目录,但这不是一个可以根据活动的构建配置自动执行的过程。

有一个开发者模式但需要手动更改(如果可以有条件地设置配置文件,也清空了instrumentationkey解决的问题)。见http://apmtips.com/blog/2015/02/02/developer-mode/

参考:http://blogs.msdn.com/b/visualstudioalm/archive/2015/01/07/application-insights-support-for-multiple-environments-stamps-and-app-versions.aspx

c# azure-application-insights
12个回答
63
投票

您可以尝试使用TelemetryConfiguration.DisableTelemetry Property这样的东西..

#if DEBUG
            TelemetryConfiguration.Active.DisableTelemetry = true;
#endif

5
投票

我们发现阻止它跟踪调试日志的最简单方法就是:

Extensibility.Implementation.TelemetryDebugWriter.IsTracingDisabled = True

3
投票

Microsoft.ApplicationInsights.AspNetCore版本2.1

services.AddApplicationInsightsTelemetry(options =>
{
    options.EnableDebugLogger = false;
});

1
投票
         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            #region Disable Application Insights debug informations
#if DEBUG
            TelemetryConfiguration.Active.DisableTelemetry = true;
            TelemetryDebugWriter.IsTracingDisabled = true;
#endif
            #endregion
//...
}

56
投票

作为对其他解决方案的补充,我建议添加以下内容让我们对Global.asax说:

protected void Application_Start()
{    
    DisableApplicationInsightsOnDebug();
    // do the other stuff
}

/// <summary>
/// Disables the application insights locally.
/// </summary>
[Conditional("DEBUG")]
private static void DisableApplicationInsightsOnDebug()
{
    TelemetryConfiguration.Active.DisableTelemetry = true;
}

这样做的好处是,它不需要对配置进行任何更改,并且它可以更好地使用像ReSharper这样的工具,它比#-directives更好地理解它。


23
投票

对于ASP.NET Core项目,App Insights默认为ON,实际上将大量信息记录到调试窗口中。

要禁用它,请转到“工具 - >选项 - >项目和解决方案 - > Web项目”,然后选中“禁用Asp.Net Core Web项目的本地应用程序洞察”。

以下是禁用本地应用洞察的图片。

有关该问题的更多信息,您可以看到官方github问题here


20
投票

正如在没有生成<instrumentationkey>key</instrumentationkey>阻止事件时部署或部署ApplicationInsights.config的问题中所解释的那样。然后,您可以将检测密钥放在代码中(仅在我的情况下发布)

#if !DEBUG
    Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = "instrumentation key";
#endif

在此调用之后创建的每个TelemetryClient都将具有正确的密钥并将跟踪事件,因此您无需在所有位置更改代码。不调用上述方法或将参数保留为空将阻止事件,因为没有配置密钥。

基本上,ApplicationInsights.config文件会覆盖设置检测密钥的任何代码,删除其中的<instrumentationkey>key</instrumentationkey>将允许您使用代码来配置密钥。如果您完全删除该文件,它将无法正常工作。

以下是确认:“如果要动态设置密钥 - 例如,如果要将应用程序的结果发送到不同的资源 - 您可以省略配置文件中的密钥,并将其设置为代码。”

参考:https://azure.microsoft.com/en-us/documentation/articles/app-insights-configuration-with-applicationinsights-config/#_instrumentationkey


12
投票

我决定使用这两种方法。我已将InstrumentationKey移至Web.config,它将被Web.Release.configWeb.Debug.config的转换所取代。 (别忘了将它从ApplicationInsights.config文件中删除)。然后我从Application_Start()调用了这个方法

public static void RegisterTelemetryInstrumentationKey()
{
    if (string.IsNullOrWhiteSpace(WebConfigurationManager.AppSettings["TelemetryInstrumentationKey"])
    {
        TelemetryConfiguration.Active.DisableTelemetry = true;
    }
    else
    {
        TelemetryConfiguration.Active.InstrumentationKey = AppSettings.TelemetryInstrumentationKey;
    }
}

10
投票

我刚才有同样的问题。

我们想控制web.config中的设置,因此在我们的应用设置中添加了一个DisableAITelemetry键:

  <appSettings>
    <add key="DisableAITelemetry" value="true" />
  </appSettings>

使用实时和演示版本,我们不会包含值(因此默认为false)。

我们可以通过添加以下内容来解决它:

bool disable;
string disableAiTelemetry = ConfigurationManager.AppSettings["DisableAITelemetry"];
bool.TryParse(disableAiTelemetry, out disable);
TelemetryConfiguration.Active.DisableTelemetry = disable;

9
投票

在ASP.NET Core应用程序中,您可以将以下内容添加到Startus.cs以关闭开发环境中的Application Insights:

if (env.IsDevelopment()) {
    TelemetryConfiguration.Active.DisableTelemetry = true;
}

builder.AddApplicationInsightsSettings();命令之后立即将其添加到构造函数中,您将不再看到AI日志堵塞调试控制台。


9
投票

使用Visual Studio 2017(15.9.2)运行ASP.NET Core 2.1 Web应用程序“禁用Asp.Net Core Web项目的本地应用程序洞察”并未清除调试窗口中的输出。

但是,在Startup.cs中将以下内容添加到Configure()可以完成这项工作;

if (_env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    TelemetryConfiguration.Active.DisableTelemetry = true;
    TelemetryDebugWriter.IsTracingDisabled = true;
}

请注意,IsTracingDisabled是关键解决方案,但我在DisableTelemetry中留下了很好的衡量标准!当在同一解决方案中搜索.NET Framework和.NET Core项目之间的类似引用时,另外两条线彼此相邻会很有帮助。


7
投票

其他一些解决方案略有不同。把它放在你的global.asax中:

Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryDebugWriter.IsTracingDisabled = Debugger.IsAttached;

它将在调试器下运行时关闭应用程序见解调试输出,但在Ctrl + F5方案下允许它并调试发布到测试服务器的构建

© www.soinside.com 2019 - 2024. All rights reserved.