net-conventions
C# coding standards for the Exceptionless codebase. Naming conventions, async patterns,structured logging, nullable reference types, and formatting rules.Keywords: C# style, naming conventions, _camelCase, PascalCase, async suffix,CancellationToken, nullable annotations, structured logging, ExceptionlessState
When & Why to Use This Skill
This Claude skill provides a comprehensive framework for .NET and C# coding standards, specifically optimized for the Exceptionless codebase. It establishes clear guidelines for naming conventions, asynchronous programming best practices, structured logging, and nullable reference types, enabling developers to maintain consistent, high-quality codebases while minimizing technical debt.
Use Cases
- Standardizing C# code style across the Exceptionless repository using defined naming conventions like _camelCase for private fields and PascalCase for public members.
- Guiding developers in implementing efficient asynchronous patterns, including the correct use of ValueTask for hot paths and CancellationToken propagation.
- Enhancing system observability by applying structured logging templates and using ExceptionlessState to provide rich diagnostic context.
- Ensuring type safety and reducing runtime errors by enforcing strict nullable reference type annotations and validation patterns.
- Streamlining the code review process by providing an automated reference for .NET best practices and Exceptionless-specific architectural rules.
| name | .NET Conventions |
|---|---|
| description | | |
| Keywords | C# style, naming conventions, _camelCase, PascalCase, async suffix, |
.NET Conventions
Style & Formatting
- Follow
.editorconfigrules strictly - Run
dotnet formatbefore committing - Minimize diffs: Change only what's necessary, preserve existing formatting and structure
- Match surrounding code style exactly
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Private fields | _camelCase |
_organizationRepository |
| Public members | PascalCase | GetByIdAsync |
| Local variables | camelCase | organizationId |
| Constants | PascalCase | MaxRetryCount |
| Type parameters | T prefix |
TModel |
Formatting Rules
- Indentation: 4 spaces, no tabs
- Namespaces: File-scoped (
namespace Foo;) - Usings: Outside namespace
- Braces: Always use, even for single-line blocks
Async Patterns
- Suffix: Always use
Asyncsuffix for async methods - CancellationToken: Pass through call chains when available
- ValueTask: Prefer
ValueTask<T>for hot paths that often complete synchronously - ConfigureAwait: Not required in ASP.NET Core
// From src/Exceptionless.Core/Services/UsageService.cs
public async Task SavePendingUsageAsync()
{
var utcNow = _timeProvider.GetUtcNow().UtcDateTime;
await SavePendingOrganizationUsageAsync(utcNow);
await SavePendingProjectUsageAsync(utcNow);
}
Structured Logging
Use message templates with named placeholders — values go in the args, not string interpolation:
// ✅ Correct: Named placeholders for structured data
_logger.LogInformation("Saving org ({OrganizationId}-{OrganizationName}) event usage",
organizationId, organization.Name);
_logger.LogError(ex, "Error retrieving event post payload: {Path}", path);
_logger.LogWarning("Unable to parse user agent {UserAgent}. Exception: {Message}",
userAgent, ex.Message);
// ❌ Wrong: String interpolation loses structure
_logger.LogInformation($"Saving org {organizationId}");
Log Scopes with ExceptionlessState
Use scopes to add context to all log entries within a block:
// From src/Exceptionless.Core/Jobs/EventPostsJob.cs
using var _ = _logger.BeginScope(new ExceptionlessState()
.Organization(ep.OrganizationId)
.Project(ep.ProjectId));
// All log entries in this scope automatically include org/project context
_logger.LogInformation("Processing event post");
Add tags and properties for richer context:
using (_logger.BeginScope(new ExceptionlessState()
.Organization(organization.Id)
.Tag("Delete")
.Tag("Bot")))
{
_logger.LogInformation("Removing bot events");
}
Nullable Reference Types
- Honor nullable annotations throughout
- Treat nullable warnings as errors
- Use
?suffix for nullable types
public async Task<User?> FindUserAsync(string? email)
{
if (string.IsNullOrWhiteSpace(email))
return null;
return await _repository.FindByEmailAsync(email);
}
Resource Disposal
// Prefer using declarations
using var stream = File.OpenRead(path);
// Async disposal
await using var connection = await CreateConnectionAsync();
Constructor Injection
Prefer constructor injection with readonly fields:
// From src/Exceptionless.Core/Services/UsageService.cs
public class UsageService
{
private readonly IOrganizationRepository _organizationRepository;
private readonly ICacheClient _cache;
private readonly TimeProvider _timeProvider;
private readonly ILogger _logger;
public UsageService(
IOrganizationRepository organizationRepository,
ICacheClient cache,
TimeProvider timeProvider,
ILoggerFactory loggerFactory)
{
_organizationRepository = organizationRepository;
_cache = cache;
_timeProvider = timeProvider;
_logger = loggerFactory.CreateLogger<UsageService>();
}
}
Validation Patterns
Input Validation
Validate early, fail fast:
public async Task<ActionResult> ProcessAsync(string id)
{
if (string.IsNullOrEmpty(id))
return BadRequest("Id is required");
var entity = await _repository.GetByIdAsync(id);
if (entity is null)
return NotFound();
// Continue processing
}
Domain Validation
See backend-architecture for validation patterns (FluentValidation for domain models, MiniValidator for API requests).