dotnet/runtime · error · Error

method instrumentation is not enabled, please enable it…

Error message

method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property

What it means

Thrown by `collectCpuSamples` after the session check passes, when the runtime was built without method instrumentation. The sampler needs the `DOTNET_WasmPerformanceInstrumentation` environment variable to be set at build time (via the `WasmPerformanceInstrumentation` MSBuild property); without it, the profiler has no instrumented methods to sample.

Solutions

  1. Add `<WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>` to the .csproj and republish.
  2. Confirm `runtimeHelpers.config.environmentVariables` actually contains the key after publish (check the generated config).
  3. Use a build configuration that enables instrumentation for profiling runs.

Example fix

// before: default publish
//  <Project Sdk="Microsoft.NET.Sdk.Web">
//   ...
//  </Project>
// collectCpuSamples() throws

// after: enable in .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>
  </PropertyGroup>
</Project>
Defensive patterns

Strategy: validation

Validate before calling

function isInstrumentationEnabled () {
  return !!runtimeHelpers.config.environmentVariables?.['DOTNET_WasmPerformanceInstrumentation'];
}
if (!isInstrumentationEnabled()) console.warn('rebuild with <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>');

Type guard

function instrumentationEnabled (env: Record<string,string|undefined>): boolean {
  return !!env?.['DOTNET_WasmPerformanceInstrumentation'];
}

Prevention

When it happens

Trigger: Calling `collectCpuSamples` (dotnet-cpu-profiler.ts:16) when `runtimeHelpers.config.environmentVariables['DOTNET_WasmPerformanceInstrumentation']` is unset. This variable is injected by the SDK only when `<WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>` is in the project.

Common situations: Default publish (no instrumentation); profiling a release build that explicitly disabled instrumentation; forgetting to add the MSBuild property before publishing.

Related errors


AI-assisted analysis of dotnet/runtime@60108ba66e (2026-08-10). Data as JSON: /api/errors/b07e0c17f27ce5d7. Report an issue: GitHub.

Appendix: source

Thrown at src/mono/browser/runtime/diagnostics/dotnet-cpu-profiler.ts:17

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { DiagnosticCommandOptions } from "../types";

import { commandStopTracing, commandSampleProfiler } from "./client-commands";
import { loaderHelpers, Module, runtimeHelpers } from "./globals";
import { serverSession, setupJsClient } from "./diagnostics-js";
import { IDiagnosticSession } from "./common";

export function collectCpuSamples (options?:DiagnosticCommandOptions):Promise<Uint8Array[]> {
    if (!options) options = {};
    if (!serverSession) {
        throw new Error("No active JS diagnostic session");
    }
    if (!runtimeHelpers.config.environmentVariables!["DOTNET_WasmPerformanceInstrumentation"]) {
        throw new Error("method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property");
    }

    const onClosePromise = loaderHelpers.createPromiseController<Uint8Array[]>();
    function onSessionStart (session: IDiagnosticSession): void {
        // stop tracing after period of monitoring
        Module.safeSetTimeout(() => {
            session.sendCommand(commandStopTracing(session.session_id));
        }, 1000 * (options?.durationSeconds ?? 60));
    }

    setupJsClient({
        onClosePromise:onClosePromise.promise_control,
        skipDownload:options.skipDownload,
        commandOnAdvertise: () => commandSampleProfiler(options),
        onSessionStart,
    });
    return onClosePromise.promise;
}

View on GitHub (pinned to 60108ba66e)