microsoft / ApplicationInsights-aspnetcore

ASP.NET Core web applications monitoring
313 stars 123 forks source link

How to turn off application insights #970

Closed ghosttie closed 5 years ago

ghosttie commented 5 years ago

Application Insights is turned on by default. I'm trying to debug a long running process, so I want it to run as fast as possible and it's spending a lot of time logging Application Insights information which I don't need.

I've followed the steps in https://github.com/aspnet/AspNetCore/issues/2051 and https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/523 and they don't work.

Repro Steps

  1. Check the option "Tools/Options/Projects and Solutions/Web Projects/Disable local Application Insights for Asp.Net Core web projects"
  2. Add to Startup.Configure:
    #if DEBUG
            TelemetryConfiguration.Active.DisableTelemetry = true;
            TelemetryDebugWriter.IsTracingDisabled = true;
    #endif

Actual Behavior

Application Insights is not disabled

Expected Behavior

Application Insights is disabled

Version Info

SDK Version: 2.7.1 .NET Core Version: 2.2 How Application was onboarded with SDK: Visual Studio OS: Windows 10 Hosting Info: Running From Visual Studio 2019 v16.2.3

cijothomas commented 5 years ago

@ghosttie Where do you see application insights turned on by default? Its is not turned on, unless you add it yourself. Depending on how you onboarded to application insights, the same can be reversed - but the most easy option is to remove application insights from packagereference.

ghosttie commented 5 years ago

I never chose it, when I created the project it was already there

I don't want to remove it completely in case we want it later, I just want to be able to turn it off when I don't need it

cijothomas commented 5 years ago

Please share the screenshots on how you created a new project. I am not aware of application insights being installed automatically for a .net core 2.2 project.

You can comment out/remove the services.AddApplicationInsightsTelemetry()/builder.UseApplicationInsights() when you don't need application insights.

ghosttie commented 5 years ago

Sorry, I guess I was misleading - it's 2.2 now but it was originally 1.0 that's been upgraded through every version, so maybe it was installed by default in the past but isn't anymore.

I tried putting the call to IWebHostBuilder.UseApplicationInsights in an #if !DEBUG but I got a

invalidoperationexception: No service for type 'Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet' has been registered.

so I figured it must be more complicated...

cijothomas commented 5 years ago

The javascriptsnippet is a known issue, fixed in 2.8.0-beta2. If you are using older version, do the following temp workaround:

Add services.AddWebEncoders(); in ConfigureServices() method of your Startup.cs class, before services.AddApplicationInsightsTelemetry();

ghosttie commented 5 years ago

I've since upgraded to ASP.NET Core 3.0 and I'm using Microsoft.ApplicationInsights.AspNetCore 2.8.0

In Startup.cs ConfigureServices I have

#if !DEBUG
    services.AddApplicationInsightsTelemetry();
#endif

but when I run it in Debug mode (or just comment it out entirely) I still get

System.InvalidOperationException: No service for type 'Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.<>c__DisplayClass8_0.<CreateActivateInfo>b__1(ViewContext context)
   at Microsoft.Extensions.Internal.PropertyActivator`1.Activate(Object instance, TContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.Activate(Object page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorPageActivator.Activate(IRazorPage page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderViewStartsAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I don't understand why it's doing anything at all if I don't call AddApplicationInsightsTelemetry...

cijothomas commented 5 years ago

@ghosttie You still have appliction insights in razor pages. If you didn't intent to use application insights, remove it from razor pages as well.

ghosttie commented 5 years ago

I also have a compile time time check around this in _ViewImports.cshtml

#if !DEBUG
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
#endif

and one in _Layout.cshtml

#if !DEBUG
@Html.Raw(JavaScriptSnippet.FullScript)
#endif

is that what you mean? Or somewhere else?

cijothomas commented 5 years ago

All usage of Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet.

The error indicates Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet is being requested (and failing as not registered). So remove all references to it.

ghosttie commented 5 years ago

Ah, apparently compiler directives don't work in CSHTML.

Thank you for your help

ghosttie commented 5 years ago

OK I'm left with my original problem - I want to be able to turn off Application Insights when in Debug mode but I can't do it.

Because compiler directives don't work in CSHTML I can't turn it off that way, and if I wrap the JavaScriptSnippet line in an @if it seems to get processed when the Razor page is compiled regardless whether the code would get called or not

Is there not a property in Application Insights to turn off collection?

I found references to TelemetryConfiguration.Active.DisableTelemetry = true but when I tried to use it I got

Warning CS0618 'TelemetryConfiguration.Active' is obsolete: 'We do not recommend using TelemetryConfiguration.Active on .NET Core. See https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152 for more details'

I read through that bug but it didn't seem to help with my goal.

Reading through the documentation I tried this to turn of all collection:

#if DEBUG
    services.ConfigureTelemetryModule<EventCounterCollectionModule>((module, o) => {
        module.Counters.Clear();
    });
#endif

but it's still logging a bunch of Application Insights Telemetry lines

cijothomas commented 5 years ago

Not sure which doc you are following.

Here's official doc: https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#disable-telemetry-dynamically