A Visual Studio Code extension that provides syntax highlighting and enhanced support for Serilog message templates and Serilog.Expressions in C#/.NET projects.
- Property names highlighted in theme-appropriate blue:
{UserId},{UserName} - Destructuring operator
@highlighted in warm orange/red:{@User} - Stringification operator
$highlighted in warm orange/red:{$Settings} - Format specifiers highlighted in green:
{Timestamp:yyyy-MM-dd} - Alignment highlighted in red:
{Name,10},{Price,-8} - Positional parameters highlighted in purple:
{0},{1} - Property braces highlighted for structure
- Multi-line verbatim strings fully supported with proper highlighting across lines
- C# 11 raw string literals supported with
"""delimiters for complex templates - Automatic theme adaptation - All colors automatically adjust for Light/Dark themes
- Filter expressions in
Filter.ByExcluding()andFilter.ByIncludingOnly() - Expression templates with control flow directives
- Operators highlighted in red:
and,or,not,like,in,is null - Functions highlighted in purple:
StartsWith(),Contains(),Length(), etc. - Keywords highlighted in blue: conditional and control flow keywords
- Literals highlighted in cyan/teal: strings, numbers, booleans, null
- Directives highlighted in magenta:
{#if},{#each},{#else},{#end} - Built-in properties highlighted in teal:
@t,@m,@l,@x,@i,@p - Theme-aware colors - All expression elements adapt to Light/Dark themes
- Works with any logger variable name (not just
_loggerorlog) - Supports both direct Serilog calls:
Log.Information(...) - Supports Microsoft.Extensions.Logging integration:
_logger.LogInformation(...) - Supports
BeginScopefor scoped logging:logger.BeginScope("Operation={Id}", operationId) - Supports
LogErrorwith exception parameter:logger.LogError(ex, "Error: {Message}", msg) - Recognizes configuration templates:
outputTemplate: "[{Timestamp:HH:mm:ss}...]"
- Immediate visual feedback as you type
- Highlighting appears as soon as you close braces
}(doesn't wait for closing quotes) - Supports incomplete strings during editing
- Light bulb suggestions when hovering over template properties
- Navigate to argument - jump from template properties to their corresponding arguments
- Click the light bulb and select "Navigate to 'PropertyName' argument"
- Highlight matching braces when cursor is positioned on
{or} - Works in all Serilog contexts: message templates, output templates, and Serilog.Expressions
- Multi-line support - matches braces across line boundaries in verbatim and raw strings
- Uses colored, bold braces for visibility without interfering with other highlighting
- Press ESC to temporarily dismiss brace matching highlights
- Synchronous highlighting of template properties and their corresponding arguments
- Position cursor on a property like
{UserId}to highlight both the property (including braces) and its argument - Position cursor on an argument to highlight it and its template property
- Includes quotes when highlighting string arguments (e.g.,
"userId") - Helps visualize the connection between template properties and their values
- Uses background fill to distinguish from brace matching
- Press ESC to temporarily dismiss property-argument highlights
- Install directly from the Visual Studio Code Marketplace
- Or from within VS Code:
- Press
Ctrl+Shift+X(orCmd+Shift+Xon Mac) to open Extensions view - Search for "Serilog Syntax Highlighting"
- Click Install
- Press
- Download the latest
.vsixfile from the releases page - In VS Code, press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) to open Command Palette - Type "Extensions: Install from VSIX..." and select it
- Choose the downloaded
.vsixfile
serilog.enabled- Enable/disable Serilog syntax highlightingserilog.propertyArgumentHighlighting- Enable/disable property-argument pair highlighting when cursor is positioned on either
The extension automatically adapts to your VS Code theme with WCAG AA compliant colors:
- Automatic theme detection - Colors change instantly when switching between Light and Dark themes
- WCAG AA accessibility - All colors maintain 4.5:1+ contrast ratios for excellent readability
- Semantic color grouping - Related elements use harmonious color families:
- Properties: Blue family (
{UserId},{Name}) - Operators: Warm colors (
@,$) - Format specifiers: Green family (
:yyyy-MM-dd) - Expression functions: Purple family (
StartsWith(),Length())
- Properties: Blue family (
You can customize colors to match your preferences:
- Open VS Code Settings (
Ctrl+,orCmd+,) - Search for "serilog" to see all available options
- Modify any of these color settings:
serilog.colors.property- Color for property names (e.g.,{Property})serilog.colors.destructure- Color for destructure operator (@)serilog.colors.stringify- Color for stringify operator ($)serilog.colors.brace- Color for braces around propertiesserilog.colors.format- Color for format specifiers (e.g.,:yyyy-MM-dd)serilog.colors.alignment- Color for alignment specifiers (e.g.,,10)serilog.colors.positional- Color for positional parameters (e.g.,{0},{1})
serilog.colors.expression.operator- Color for expression operators (and,or,not, etc.)serilog.colors.expression.function- Color for expression functions (Contains,StartsWith, etc.)serilog.colors.expression.builtin- Color for built-in properties (@t,@l,@m, etc.)serilog.colors.expression.directive- Color for directives (#if,#else,#each, etc.)serilog.colors.expression.string- Color for string literals in expressionsserilog.colors.expression.number- Color for number literals in expressionsserilog.colors.expression.keyword- Color for keywords (null,true,false, etc.)serilog.colors.expression.identifier- Color for identifiers/property names in expressions
Note: Custom colors override the automatic theme-aware colors.
After installation, the extension works automatically - no configuration required!
- Open any C# file that uses Serilog logging
- Start typing a Serilog log statement:
_logger.LogInformation("User {UserId} logged in", userId);
- See instant highlighting as you type - properties turn blue, operators orange, etc.
Create a new C# file and paste this to see all features:
using Serilog;
Log.Information("User {UserId} logged in with {@Details} at {Timestamp:HH:mm:ss}",
userId, userDetails, DateTime.Now);You should see:
UserIdin blue (adapts to your theme)@in warm orange/red,Detailsin blueTimestampin blue,:HH:mm:ssin green- Colors automatically match your Light/Dark theme preference
The extension recognizes and highlights all Serilog message template features:
// Basic properties
logger.LogInformation("User {UserId} logged in at {LoginTime}", userId, loginTime);
// Destructuring (captures object structure)
logger.LogInformation("Processing user {@User}", user);
// Stringification (forces string representation)
logger.LogInformation("Configuration loaded {$Settings}", settings);
// Format specifiers
logger.LogInformation("Current time: {Timestamp:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
// Alignment
logger.LogInformation("Item: {Name,10} | Price: {Price,8:C}", name, price);
// Positional parameters (legacy support)
logger.LogWarning("Error {0} occurred in {1}", errorCode, methodName);
// Multi-line verbatim strings
logger.LogInformation(@"Processing report:
User: {UserName}
Department: {Department}
Generated: {Timestamp:yyyy-MM-dd}", userName, dept, DateTime.Now);
// C# 11 raw string literals (no escaping needed for quotes)
logger.LogInformation("""
Processing record:
ID: {RecordId}
Status: {Status}
User: {@User}
""", recordId, status, user);
// Configuration templates
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}")
.CreateLogger();
// Serilog.Expressions filter syntax
Log.Logger = new LoggerConfiguration()
.Filter.ByExcluding("RequestPath like '/health%'")
.Filter.ByIncludingOnly("Level = 'Error' or StatusCode >= 500")
.CreateLogger();
// Expression templates with control flow
var expressionTemplate = new ExpressionTemplate(
"{#if Level = 'Error'}β{#else}β
{#end} {@m}");
// Computed properties with expressions
.Enrich.WithComputed("ResponseTime", "EndsWith(RequestPath, '.json') ? Elapsed * 2 : Elapsed")The extension automatically detects Serilog calls regardless of how you name your logger variables:
// All of these work automatically
_logger.LogInformation("Message with {Property}", value);
logger.LogDebug("Debug message with {Data}", data);
myCustomLogger.LogWarning("Warning with {Details}", details);
log.LogError("Error with {Context}", context);Access these commands via the Command Palette (Ctrl+Shift+P or Cmd+Shift+P):
- Serilog: Test Extension - Verify the extension is working
- Serilog: Refresh Highlighting - Refresh syntax highlighting in the current file
- VS Code
- Node.js 18+
- npm
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Run tests
npm test
# Run benchmarks
npm run benchmark
# Watch mode for development
npm run watch
# Package extension
npm run packageThe extension uses VS Code's extensibility APIs:
- TextEditorDecorationType API - For syntax highlighting via decorations
- Configuration API - For user customization settings
- Theme API - For automatic theme adaptation
Key components:
templateParser.ts- Parses Serilog message templatesexpressionParser.ts&expressionTokenizer.ts- Handles Serilog.Expressions syntaxstringLiteralParser.ts- Detects C# string literals (regular, verbatim, raw)serilogDetector.ts- Identifies Serilog method calls in C# codecacheManager.ts- LRU cache for parsed templates providing performance optimizationthemeManager.ts- Theme-aware color managementdecorationManager.ts- Manages text decorations for highlighting
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new features
- Run the test suite:
npm test - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.