仅在Release中启用ApplicationInsights

时间:2017-10-27 12:37:52

标签: c# razor asp.net-core azure-application-insights

在我的ASP.NET核心应用程序中,我想仅为发布/生产启用ApplicationInsights。为此,我需要有条件地使用三行代码。前两个是`_layout.cshtml

的一部分
#if DEBUG
#else
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet @* does not work this way *@
#endif
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>@ViewData["Title"]</title>
    <link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
#if DEBUG
#else
@Html.Raw(JavaScriptSnippet.FullScript) @* does not work this way *@
#endif
</head>

<body style='font-family:"-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, \"Helvetica Neue\", Arial, sans-serif;"'>
    @await Component.InvokeAsync(typeof(Bsoft.Core.Mvc.BannerViewComponent))
    @RenderBody()

    @RenderSection("scripts", required: false)

</body>
</html>

我尝试使用#if,但它不起作用。 #if将显示为html。

第二部分是program.cs中的.UseApplicationInsights()

public class Program
{
  public static void Main(string[] args)
  {
    var host = BuildWebHost(args);
    host.Run();
  }

  public static IWebHost BuildWebHost(string[] args)
  {
    var config = new ConfigurationBuilder()
        .AddEnvironmentVariables(prefix: "ASPNETCORE_")
        .Build();
    return new WebHostBuilder()
      .UseConfiguration(config)
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseIISIntegration()
      .UseStartup<Startup>()
#if DEBUG
#else
      .UseApplicationInsights() // <= only in release/production 
#endif
      .Build();
  }
}

对于.UseApplicationInsights(),我可以使用#ifndef DEBUG,但如何才能在发布中应用_layout.cshtml中的更改?

0 个答案:

没有答案