Skip to content

Backport explicit Property Names such as @Excepetion #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ using object literals:
The following properties are available in expressions:

* **All first-class properties of the event** - no special syntax: `SourceContext` and `Cart` are used in the formatting examples above
* `@t` - the event's timestamp, as a `DateTimeOffset`
* `@m` - the rendered message
* `@mt` - the raw message template
* `@l` - the event's level, as a `LogEventLevel`
* `@x` - the exception associated with the event, if any, as an `Exception`
* `@p` - a dictionary containing all first-class properties; this supports properties with non-identifier names, for example `@p['snake-case-name']`
* `@t` or `@TimeStamp` - the event's timestamp, as a `DateTimeOffset`
* `@m` or `@Message` - the rendered message
* `@mt` or `@MessageTemplate` - the raw message template
* `@l` or `@Level` - the event's level, as a `LogEventLevel`
* `@x` @or `@Exception` - the exception associated with the event, if any, as an `Exception`
* `@p` or `@Properties` - a dictionary containing all first-class properties; this supports properties with non-identifier names, for example `@p['snake-case-name']`
* `@i` - event id; a 32-bit numeric hash of the event's message template
* `@r` - renderings; if any tokens in the message template include .NET-specific formatting, an array of rendered values for each such token

Expand Down
10 changes: 10 additions & 0 deletions src/Serilog.Expressions/Expressions/BuiltInProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ static class BuiltInProperty
public const string Properties = "p";
public const string Renderings = "r";
public const string EventId = "i";

// These were the friendlier names that was available in
// Serilog.Filters.Expressions
public const string ExceptionExplicit = "Exception";
public const string LevelExplicit = "Level";
public const string TimestampExplicit = "Timestamp";
public const string MessageExplicit = "Message";
public const string MessageTemplateExplicit = "MessageTemplate";
public const string PropertiesExplicit = "Properties";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,29 @@ protected override ExpressionBody Transform(AmbientNameExpression px)
return px.PropertyName switch
{
BuiltInProperty.Level => Splice(context => new ScalarValue(context.LogEvent.Level)),
BuiltInProperty.LevelExplicit => Splice(context => new ScalarValue(context.LogEvent.Level)),

BuiltInProperty.Message => Splice(context => new ScalarValue(Intrinsics.RenderMessage(formatter, context))),
BuiltInProperty.MessageExplicit => Splice(context => new ScalarValue(Intrinsics.RenderMessage(formatter, context))),

BuiltInProperty.Exception => Splice(context =>
context.LogEvent.Exception == null ? null : new ScalarValue(context.LogEvent.Exception)),
BuiltInProperty.ExceptionExplicit => Splice(context =>
context.LogEvent.Exception == null ? null : new ScalarValue(context.LogEvent.Exception)),

BuiltInProperty.Timestamp => Splice(context => new ScalarValue(context.LogEvent.Timestamp)),
BuiltInProperty.TimestampExplicit => Splice(context => new ScalarValue(context.LogEvent.Timestamp)),

BuiltInProperty.MessageTemplate => Splice(context => new ScalarValue(context.LogEvent.MessageTemplate.Text)),
BuiltInProperty.MessageTemplateExplicit => Splice(context => new ScalarValue(context.LogEvent.MessageTemplate.Text)),

BuiltInProperty.Properties => Splice(context =>
new StructureValue(context.LogEvent.Properties.Select(kvp => new LogEventProperty(kvp.Key, kvp.Value)),
null)),
BuiltInProperty.PropertiesExplicit => Splice(context =>
new StructureValue(context.LogEvent.Properties.Select(kvp => new LogEventProperty(kvp.Key, kvp.Value)),
null)),

BuiltInProperty.Renderings => Splice(context => Intrinsics.GetRenderings(context.LogEvent, formatProvider)),
BuiltInProperty.EventId => Splice(context =>
new ScalarValue(EventIdHash.Compute(context.LogEvent.MessageTemplate.Text))),
Expand Down
16 changes: 16 additions & 0 deletions src/Serilog.Expressions/Templates/Compilation/TemplateCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@ public static CompiledTemplate Compile(Template template,
LiteralText text => new CompiledLiteralText(text.Text, theme),
FormattedExpression { Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.Level} } level => new CompiledLevelToken(
level.Format, level.Alignment, theme),
FormattedExpression { Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.LevelExplicit } } level => new CompiledLevelToken(
level.Format, level.Alignment, theme),

FormattedExpression
{
Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.Exception },
Alignment: null,
Format: null
} => new CompiledExceptionToken(theme),
FormattedExpression
{
Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.ExceptionExplicit },
Alignment: null,
Format: null
} => new CompiledExceptionToken(theme),

FormattedExpression
{
Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.Message },
Format: null
} message => new CompiledMessageToken(formatProvider, message.Alignment, theme),
FormattedExpression
{
Expression: AmbientNameExpression { IsBuiltIn: true, PropertyName: BuiltInProperty.MessageExplicit },
Format: null
} message => new CompiledMessageToken(formatProvider, message.Alignment, theme),

FormattedExpression expression => new CompiledFormattedExpression(
ExpressionCompiler.Compile(expression.Expression, formatProvider, nameResolver), expression.Format, expression.Alignment, formatProvider, theme),
TemplateBlock block => new CompiledTemplateBlock(block.Elements.Select(e => Compile(e, formatProvider, nameResolver, theme)).ToArray()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
Hello, {'world'}! ⇶ Hello, world!
{@l} ⇶ Information
{@l:u3} ⇶ INF
{@Level} ⇶ Information
{@Level:u4} ⇶ INFO
{@Level:w3} ⇶ inf
Items are {[1, 2]} ⇶ Items are [1,2]
Members are { {a: 1, 'b c': 2} } ⇶ Members are {"a":1,"b c":2}
{@p} ⇶ {"Name":"nblumhardt"}
{@p['Name']} ⇶ nblumhardt
{@Properties} ⇶ {"Name":"nblumhardt"}
{@Properties['Name']} ⇶ nblumhardt
{@mt} ⇶ Hello, {Name}!
{@MessageTemplate} ⇶ Hello, {Name}!
Hello, {'my } brackety { {}} friends'}! ⇶ Hello, my } brackety { {}} friends!
Text only ⇶ Text only
{{ Escaped {{ left {{ ⇶ { Escaped { left {
Expand All @@ -13,6 +21,7 @@ Aligned {42,4}! ⇶ Aligned 42!
Left {42,-4}! ⇶ Left 42 !
Under width {42,0}! ⇶ Under width 42!
{@m} ⇶ Hello, nblumhardt!
{@Message} ⇶ Hello, nblumhardt!
Hello, {#if true}world{#end}! ⇶ Hello, world!
Hello, {#if true}w{42}d{#end}! ⇶ Hello, w42d!
Hello, {#if 1 = 1}world{#else}there{#end}! ⇶ Hello, world!
Expand Down