# What a .NET 11 container can tell you just before it dies

When a worker disappears halfway through a batch the container restarts, the next batch runs, and the last useful log line is "Processing messages". By the time you investigate, the process that could tell you what happened has gone. .NET 11 gives Linux containers another way to leave evidence behind. CoreCLR can write a JSON crash report from inside the failing process, with managed stack frames and runtime details that can help you work out where it died. The container still exits. Whether you get to read the report depends on where you told the runtime to write it.

## What changed in .NET 11?

.NET already had crash diagnostics on Linux through the external `createdump` helper. The in-process reporter started on mobile and is now available on Linux and macOS. If minidump collection is disabled, setting `DOTNET_EnableCrashReportOnly=1` selects the in-process reporter. If you enable minidumps, .NET keeps the existing `createdump` behaviour. That distinction is easy to miss when you copy environment variables from an older container image. A crash report gives you thread and stack information, loaded modules and some runtime state. A memory dump gives you a different kind of evidence when you need to inspect process memory, for example to investigate heap corruption. For a first pass at a recurring crash, being able to pull down a JSON file and look for the relevant managed frames is often enough to decide where to look next.

## Crash a worker on purpose

The demo uses `Environment.FailFast` to stop the process at a known point. That gives us a named method to look for in the report. On Linux, `FailFast` also writes its message and stack trace to standard error, so you can compare what the container logs capture with what the runtime writes to disk.

Create a console app with the .NET 11 SDK.

```bash
dotnet new console --name CrashDemo --framework net11.0
cd CrashDemo
```

Replace `Program.cs` with this.

```csharp
using System;
using System.Runtime.CompilerServices;

SimulateFatalFailure();

[MethodImpl(MethodImplOptions.NoInlining)]
static void SimulateFatalFailure()
{
    Environment.FailFast("Demo crash while processing a batch.");
}
```

Build a framework-dependent app and run it on the .NET 11 runtime image. Save this as `Dockerfile` next to `CrashDemo.csproj`.

```dockerfile
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /src
COPY CrashDemo.csproj ./
RUN dotnet restore CrashDemo.csproj
COPY Program.cs ./
RUN dotnet publish CrashDemo.csproj -c Release --no-restore -o /out

FROM mcr.microsoft.com/dotnet/runtime:11.0
WORKDIR /app
COPY --from=build /out/ ./
ENTRYPOINT ["dotnet", "CrashDemo.dll"]
```

Build the image and give the reporter a directory on the host. The demo uses your host UID and GID so the container can write to that directory without making it world-writable.

```bash
docker build -t dotnet11-crash-demo .
reports_dir=$(mktemp -d "$PWD/crash-reports.XXXXXX")

docker run --rm \
  --user "$(id -u):$(id -g)" \
  --mount "type=bind,src=$reports_dir,dst=/reports" \
  --env DOTNET_DbgEnableMiniDump=0 \
  --env DOTNET_EnableCrashReportOnly=1 \
  --env DOTNET_DbgMiniDumpName='/reports/crash.%h.%p.%t' \
  dotnet11-crash-demo
```

The run is supposed to exit with an error. Run the next commands separately if your shell exits on a failed command. The runtime appends `.crashreport.json` to the configured name. The hostname, PID and timestamp help distinguish reports from different container runs. The PID by itself is a poor choice here because `dotnet` can be PID 1 in every new container.

```bash
find "$reports_dir" -name '*.crashreport.json' -print
find "$reports_dir" -name '*.crashreport.json' -exec jq 'keys' {} \;
find "$reports_dir" -name '*.crashreport.json' \
  -exec jq -r '.. | strings | select(contains("SimulateFatalFailure"))' {} \;
```

The first command checks that the report survived the container’s removal. The second checks that it’s readable JSON and shows its top level fields. The third looks for the frame we deliberately left on the managed stack. If the directory is empty, check the container’s stderr, the runtime version and whether the process can write to the mounted path.

## The process gets one chance to write the file

The bind mount is the part of the example I'd carry into a real deployment. With `docker run --rm`, the container is removed when it exits. Anything written solely to its writable layer goes with it. Restarting the *same* Docker container preserves that layer, but replacing the container gives you a fresh one. On Kubernetes, an `emptyDir` can survive a container restart within the same Pod. If the Pod itself goes away, that directory goes with it. A collector or persistent volume gives the report a life beyond the Pod. Theres also a limit to what the runtime can capture. If Linux sends `SIGKILL`, as can happen when the kernel kills a process for exceeding its memory limit, the process gets no opportunity to run the reporter. For that incident, start with the container’s termination reason and memory telemetry. For a fatal error that reaches CoreCLR, look for the JSON report alongside the last application logs. The test I'd put in a deployment runbook is simple. Crash a disposable replica, let the platform replace it, then check whether you can still retrieve the report. That tells you whether the evidence will be there when you need it.

[Collect dumps on crash](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/collect-dumps-crash)

[What's new in the .NET 11 runtime](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#in-process-crash-report-logging)

[Environment.FailFast](https://learn.microsoft.com/en-us/dotnet/api/system.environment.failfast?view=net-11.0)

[Crash report file path templates](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/collect-dumps-crash#file-path-templates)

[Docker storage](https://docs.docker.com/engine/storage/)

[Kubernetes volumes](https://kubernetes.io/docs/concepts/storage/volumes/#emptydir)

[Linux signal behaviour](https://man7.org/linux/man-pages/man7/signal.7.html)
