dotnet/AspNetCore.Docs · warning · OperationCanceledException

Skip 3

Error message

Skip 3

What it means

Intentionally-thrown OperationCanceledException("Skip 3") in the Counter sample to demonstrate logging exception parameters. After incrementing currentCount to 3 it is forced to 4 and the exception is thrown, then caught and logged via logger.LogWarning(ex, "...{Count}...", currentCount) so the developer-console output shows currentCount: 4. Teaching code for structured logging of exceptions.

Source

Thrown at aspnetcore/blazor/fundamentals/logging.md:171

> :::no-loc text="Someone clicked me at 04/21/2022 12:15:57!":::

## Client-side log exception parameters

[Log exception parameters](xref:fundamentals/logging/index#log-exceptions) are supported.

The following example shows how to use log exception parameters with the `Counter` component of an app created from a Blazor project template.

In the `IncrementCount` method of the app's `Counter` component (`Counter.razor`):

```csharp
currentCount++;

try
{
    if (currentCount == 3)
    {
        currentCount = 4;
        throw new OperationCanceledException("Skip 3");
    }
}
catch (Exception ex)
{
    logger.LogWarning(ex, "Exception (currentCount: {Count})!", currentCount);
}
```

Developer tools console output:

> :::no-loc text="warn: BlazorSample.Pages.Counter[0]":::  
> :::no-loc text="Exception (currentCount: 4)!":::  
> :::no-loc text="System.OperationCanceledException: Skip 3":::  
> :::no-loc text="at BlazorSample.Pages.Counter.IncrementCount() in C:\Users\Alaba\Desktop\BlazorSample\Pages\Counter.razor:line 28":::

## Client-side filter function

[Filter functions](xref:fundamentals/logging/index#filter-function) are supported.

View on GitHub (pinned to c67a80103a)

Solutions

  1. Recognize this is illustrative; remove the artificial throw for real counter logic.
  2. If demonstrating logging, keep the throw but ensure the catch uses the correct log level and parameter placeholders.
  3. Verify ILogger is injected (the catch must not throw NullReferenceException if logger is null).

Example fix

// before
if (currentCount == 3)
{
    currentCount = 4;
    throw new OperationCanceledException("Skip 3");
}

// after — log a real skip condition without throwing
currentCount++;
if (currentCount == 3)
{
    currentCount = 4;
    logger.LogWarning("Skipped count of 3; currentCount now {Count}.", currentCount);
}
Defensive patterns

Strategy: try-catch

Validate before calling

currentCount++;
if (currentCount == 3) { currentCount = 4; /* log instead of throw */ }

Try / catch

try { /* increment */ }
catch (Exception ex) when (ex is OperationCanceledException)
{
    logger.LogWarning(ex, "Skipped count (currentCount: {Count}).", currentCount);
}

Prevention

When it happens

Trigger: Clicking increment until currentCount equals 3; the sample sets it to 4 and throws. The catch logs the warning with the parameterized count.

Common situations: Running the logging sample to observe warn-level console output; copying it to learn ILogger usage with exception and format arguments.

Related errors


AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13). Data as JSON: /api/errors/3f4f199270bc5ea3. Report an issue: GitHub.