{"record":{"id":"b07e0c17f27ce5d7","repo":"dotnet/runtime","slug":"method-instrumentation-is-not-enabled-please-enab","errorCode":null,"errorMessage":"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property","messagePattern":"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mono/browser/runtime/diagnostics/dotnet-cpu-profiler.ts","lineNumber":17,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport type { DiagnosticCommandOptions } from \"../types\";\n\nimport { commandStopTracing, commandSampleProfiler } from \"./client-commands\";\nimport { loaderHelpers, Module, runtimeHelpers } from \"./globals\";\nimport { serverSession, setupJsClient } from \"./diagnostics-js\";\nimport { IDiagnosticSession } from \"./common\";\n\nexport function collectCpuSamples (options?:DiagnosticCommandOptions):Promise<Uint8Array[]> {\n    if (!options) options = {};\n    if (!serverSession) {\n        throw new Error(\"No active JS diagnostic session\");\n    }\n    if (!runtimeHelpers.config.environmentVariables![\"DOTNET_WasmPerformanceInstrumentation\"]) {\n        throw new Error(\"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property\");\n    }\n\n    const onClosePromise = loaderHelpers.createPromiseController<Uint8Array[]>();\n    function onSessionStart (session: IDiagnosticSession): void {\n        // stop tracing after period of monitoring\n        Module.safeSetTimeout(() => {\n            session.sendCommand(commandStopTracing(session.session_id));\n        }, 1000 * (options?.durationSeconds ?? 60));\n    }\n\n    setupJsClient({\n        onClosePromise:onClosePromise.promise_control,\n        skipDownload:options.skipDownload,\n        commandOnAdvertise: () => commandSampleProfiler(options),\n        onSessionStart,\n    });\n    return onClosePromise.promise;\n}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/runtime/blob/290d5ab72cc1102813fbc0406fb186ceadabc340/src/mono/browser/runtime/diagnostics/dotnet-cpu-profiler.ts#L1-L35","documentation":"Thrown by collectCpuSamples() in diagnostics/dotnet-cpu-profiler.ts when the runtime configuration does not have the environment variable DOTNET_WasmPerformanceInstrumentation set. The WASM CPU profiler needs method-level instrumentation emitted into the assemblies at build time; that instrumentation is only produced when the WasmPerformanceInstrumentation MSBuild property is true, which also sets the corresponding env var. Without it there is nothing for the profiler to sample, so the call is rejected up front.","triggerScenarios":"Calling API.collectCpuSamples() on an app that was published without <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> in the project file. Profiling a release build where instrumentation was stripped.","commonSituations":"Trying to profile a normally-published WASM app. Forgetting the MSBuild property in the .csproj. Publishing in Release which may omit instrumentation.","solutions":["Add <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> to your .csproj (inside a PropertyGroup) and republish.","Confirm runtimeHelpers.config.environmentVariables['DOTNET_WasmPerformanceInstrumentation'] is set after load (proves the build emitted it).","Rebuild/reload the app so the instrumented assemblies and env var are served."],"exampleFix":"<!-- before: .csproj has no instrumentation -->\n<PropertyGroup>\n  <TargetFramework>net9.0</TargetFramework>\n</PropertyGroup>\n\n<!-- after -->\n<PropertyGroup>\n  <TargetFramework>net9.0</TargetFramework>\n  <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>\n</PropertyGroup>","handlingStrategy":"validation","validationCode":"function canProfileCpu(): boolean {\n  return !!runtimeHelpers.config.environmentVariables?.[\"DOTNET_WasmPerformanceInstrumentation\"];\n}\n// publish with <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> before relying on this","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Set <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> in .csproj and republish.","Verify the env var is present at runtime before calling collectCpuSamples."],"tags":["diagnostics","dotnet-cpu-profiler","msbuild","configuration"],"analyzedSha":"290d5ab72cc1102813fbc0406fb186ceadabc340","analyzedAt":"2026-08-06T19:57:01.276Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}