Skip to content

feat(logging): add structured logging helpers with strong typing #15

Description

@CalvinAllen

Summary

Add helpers for creating strongly-typed, structured log events. Reduces string formatting errors and enables better log analysis in observability platforms.

Problem

Current logging requires manual string formatting and doesn't enforce structure:

// Easy to make mistakes, no compile-time checking
logger.LogInformation("User {UserId} performed {Action} on {Resource}", 
    userId, action, resource);

Proposed Solution

Option 1: Source Generator Approach

// Define log events as partial methods
public static partial class LogEvents
{
    [LogEvent(Level = LogLevel.Information, Message = "User {UserId} performed {Action}")]
    public static partial void UserAction(this ILogger logger, string userId, string action);
    
    [LogEvent(Level = LogLevel.Error, Message = "Operation {Operation} failed")]
    public static partial void OperationFailed(this ILogger logger, string operation, Exception ex);
}

// Usage - strongly typed!
VsixTelemetry.Logger.UserAction("user123", "save");
VsixTelemetry.Logger.OperationFailed("LoadProject", exception);

Option 2: Fluent Builder

VsixTelemetry.Log
    .Information("User performed action")
    .WithProperty("UserId", userId)
    .WithProperty("Action", action)
    .WithProperty("Resource", resource)
    .Write();

// Or for common patterns
VsixTelemetry.Log
    .Operation("LoadSolution")
    .Started();
    
VsixTelemetry.Log
    .Operation("LoadSolution") 
    .Succeeded(duration);
    
VsixTelemetry.Log
    .Operation("LoadSolution")
    .Failed(exception);

Option 3: Event Classes

public record UserActionEvent(string UserId, string Action, string Resource);

VsixTelemetry.LogEvent(new UserActionEvent("user123", "save", "document.cs"));
// Automatically extracts properties and formats message

Benefits

  • Compile-time checking of log parameters
  • Consistent property naming
  • Better IntelliSense support
  • Easier log analysis (consistent structure)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions