Skip to content

ORLEANS0215: Log through a replay-safe logger

PropertyValue
SeverityInfo
CategoryUsage
Code fixNone

A durable task body logs through an ILogger, with a LoggerExtensions method, ILogger.Log, or a [LoggerMessage] method, at a point from which it still reaches a durable operation, and the logger isn’t one which ReplaySafe() returns:

public sealed class ExportGrain(ILogger<ExportGrain> logger) : DurableGrain
{
public async DurableTask<int> ExportAsync(string[] partitions)
{
foreach (var partition in partitions)
{
// Logged again for every completed partition each time the task replays.
logger.LogInformation("Exporting partition {Partition}.", partition);
await Export(partition).WithId("partition");
}
return partitions.Length;
}
private static async DurableTask<bool> Export(string partition) => partition.Length > 0;
}

A replay runs the code before the body’s last durable operation again, including its log calls. After the export in the example deactivates during its tenth partition, the replay logs Exporting partition for the nine completed partitions again, and logs repeat each time the task replays. Logs then overstate the work, and log-based metrics and alerts count it again.

A logger which ReplaySafe() returns skips events while the task replays: from the start of a run which finds recorded operations, until the run reaches an operation which no earlier run recorded. Each event is then logged by the run which first reaches it. When an activation ends before that run’s write commits, the next run repeats the uncommitted work and its logs, so these logs suit diagnostics, and exactly-once metrics or audits belong in recorded results.

For replay-safe logging, see Replay and determinism.

Wrap the logger with ReplaySafe(), typically once in the grain’s constructor. The logger reads the running task when it logs, so ordinary grain methods use it too:

public sealed class ReplaySafeExportGrain(ILogger<ReplaySafeExportGrain> logger) : DurableGrain
{
// Skips events while a task replays, so each is logged once. Ordinary grain methods can use the same logger.
private readonly ILogger _logger = logger.ReplaySafe();
public async DurableTask<int> ExportAsync(string[] partitions)
{
foreach (var partition in partitions)
{
_logger.LogInformation("Exporting partition {Partition}.", partition);
await Export(partition).WithId("partition");
}
return partitions.Length;
}
private static async DurableTask<bool> Export(string partition) => partition.Length > 0;
}

Logs in a DurableTask.RunInline effect, in a step, or in the body’s synchronous tail repeat only until their results commit: a replay returns a recorded result without running its code, and code whose record didn’t commit before the activation ended runs, and logs, again.

Suppress the diagnostic for logs which should record every run, such as debugging logs which show where replays happen.