Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 10, 2025

Orchestrations must use context.CreateReplaySafeLogger() instead of injecting ILogger directly, otherwise logs will be emitted during replay operations. This was listed as a common gotcha in #253 but lacked compile-time detection.

Changes

  • New analyzer LoggerOrchestrationAnalyzer (DURABLE0009) warns when:

    • ILogger or ILogger<T> parameters appear in orchestration signatures
    • ILogger fields/properties are referenced within orchestration code paths
    • Works across Durable Functions, TaskOrchestrator, and AddOrchestratorFunc patterns
  • Detection logic traverses method call chains to catch indirect usage, skips parameter references already flagged at declaration to avoid duplicate diagnostics

  • Ignores legitimate cases: Activity functions, loggers created via CreateReplaySafeLogger(), non-orchestration code

Example

// ❌ Warns: DURABLE0009
[Function("MyOrchestration")]
public async Task Run(
    [OrchestrationTrigger] TaskOrchestrationContext context,
    ILogger logger)  // Non-contextual logger detected
{
    logger.LogInformation("Processing");  // Will log during replay
}

// ✅ Correct usage
[Function("MyOrchestration")]
public async Task Run([OrchestrationTrigger] TaskOrchestrationContext context)
{
    ILogger logger = context.CreateReplaySafeLogger(nameof(Run));
    logger.LogInformation("Processing");  // Only logs when not replaying
}

Test coverage includes all orchestration types, generic logger variants, field/property references, and method chains.

Original prompt

This section details on the original issue you should resolve

<issue_title>Roslyn Analyzer should alert against using a non-contextual logger</issue_title>
<issue_description>Originally reported by #253</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 10, 2025 19:35
Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
…ate diagnostics

Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Comment on lines +69 to +81
foreach (IParameterSymbol parameter in methodSymbol.Parameters)
{
if (this.IsILoggerType(parameter.Type))
{
// Found an ILogger parameter - report diagnostic at the parameter location
if (parameter.DeclaringSyntaxReferences.Length > 0)
{
SyntaxNode parameterSyntax = parameter.DeclaringSyntaxReferences[0].GetSyntax();
reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, parameterSyntax, methodSymbol.Name, orchestrationName));
reportedParameters.Add(parameter);
}
}
}
Comment on lines +71 to +80
if (this.IsILoggerType(parameter.Type))
{
// Found an ILogger parameter - report diagnostic at the parameter location
if (parameter.DeclaringSyntaxReferences.Length > 0)
{
SyntaxNode parameterSyntax = parameter.DeclaringSyntaxReferences[0].GetSyntax();
reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, parameterSyntax, methodSymbol.Name, orchestrationName));
reportedParameters.Add(parameter);
}
}
Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Roslyn Analyzer alert for non-contextual logger usage Add Roslyn analyzer for non-contextual logger usage in orchestrations (DURABLE0009) Dec 10, 2025
Copilot AI requested a review from YunchuWang December 10, 2025 19:47
Comment on lines +134 to +140
foreach (INamedTypeSymbol interfaceType in namedType.AllInterfaces)
{
if (SymbolEqualityComparer.Default.Equals(interfaceType, this.iLoggerSymbol))
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Roslyn Analyzer should alert against using a non-contextual logger

2 participants