fix: Support LogError calls with exception parameters for syntax highlighting#13
Merged
willibrandon merged 2 commits intomainfrom Sep 9, 2025
Merged
Conversation
…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
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
IsLogErrorWithExceptionParameter()method that analyzes parameter structure by tracking parenthesis depth and comma positions to detect LogError patterns with exception parametersFindStringLiteral()withskipFirstStringparameter to skip the exception parameter and locate the actual message templatenew Exception("literal")) and variable constructors (new Exception(variable)) through comma detection logicType of change
Checklist
.\scripts\test.ps1)Additional notes
Fixes #12