Skip to content

fix: Support LogError calls with exception parameters for syntax highlighting#13

Merged
willibrandon merged 2 commits intomainfrom
fix/logerror-exception-parameter-highlighting
Sep 9, 2025
Merged

fix: Support LogError calls with exception parameters for syntax highlighting#13
willibrandon merged 2 commits intomainfrom
fix/logerror-exception-parameter-highlighting

Conversation

@willibrandon
Copy link
Owner

Description

LogError calls with exception parameters like logger.LogError(new Exception("error"), "User {UserId} failed", userId) were not getting syntax highlighting for the message template properties, while the exception constructor string was incorrectly being highlighted instead.

Root cause: The string literal parser in FindStringLiteral() was treating the first string literal found (exception constructor parameter) as the message template, instead of identifying that LogError with exception parameters has the actual message template as the second parameter.

Solution:

  • Added IsLogErrorWithExceptionParameter() method that analyzes parameter structure by tracking parenthesis depth and comma positions to detect LogError patterns with exception parameters
  • Enhanced FindStringLiteral() with skipFirstString parameter to skip the exception parameter and locate the actual message template
  • Handles both string literal exception constructors (new Exception("literal")) and variable constructors (new Exception(variable)) through comma detection logic
  • Added tests covering both LogError exception parameter scenarios

Type of change

  • Bug fix
  • New feature
  • Performance improvement
  • Documentation update

Checklist

  • Tests pass (.\scripts\test.ps1)
  • Benchmarks checked (if performance-related)
  • Documentation updated (if needed)

Additional notes

Fixes #12

…lighting

- Add detection logic to identify LogError(exception, template, args) pattern
- Skip first parameter when exception is present to find message template
- Handle both string literal and variable exception constructors
- Add tests for both scenarios
- Include examples in ExampleService.cs
@willibrandon willibrandon self-assigned this Sep 9, 2025
@willibrandon willibrandon added bug Something isn't working visual-studio Visual Studio extension functionality and integration labels Sep 9, 2025
@willibrandon willibrandon requested a review from Copilot September 9, 2025 05:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a syntax highlighting bug where LogError calls with exception parameters were incorrectly highlighting the exception constructor string instead of the actual message template. The fix adds logic to detect LogError patterns with exception parameters and skip the first string literal to find the correct message template.

  • Added detection for LogError calls with exception parameters to distinguish them from regular LogError calls
  • Enhanced string literal parsing to optionally skip the first string literal when an exception parameter is present
  • Added comprehensive test coverage for both string literal and variable exception constructor scenarios

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
SerilogSyntax/Parsing/StringLiteralParser.cs Enhanced FindStringLiteral method with skipFirstString parameter and logic to skip exception constructor strings
SerilogSyntax/Classification/SerilogClassifier.cs Added IsLogErrorWithExceptionParameter method to detect exception parameter patterns and integrate with enhanced parser
SerilogSyntax.Tests/Classification/SerilogClassifierTests.cs Added test cases for LogError with exception parameters using both string literals and variables
Example/ExampleService.cs Added example code demonstrating LogError calls with exception parameters

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

- Test LogError with raw string literal in exception parameter
- Test that exception properties are not highlighted when message template properties should be
- Ensure proper discrimination between exception and message template strings
@willibrandon willibrandon merged commit 598dcd9 into main Sep 9, 2025
1 check passed
@willibrandon willibrandon deleted the fix/logerror-exception-parameter-highlighting branch September 9, 2025 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working visual-studio Visual Studio extension functionality and integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax highlighting fails for LogError calls with exception parameter

1 participant