Skip to content

Commit 8e6e31e

Browse files
committed
Update the examples a little
1 parent 7e11741 commit 8e6e31e

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

example/Sample/Program.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,22 @@ public static void Main()
1212
{
1313
SelfLog.Enable(Console.Error);
1414

15-
TextFormattingExample();
15+
TextFormattingExample1();
1616
JsonFormattingExample();
1717
PipelineComponentExample();
18+
TextFormattingExample2();
1819
}
1920

20-
static void TextFormattingExample()
21+
static void TextFormattingExample1()
2122
{
2223
using var log = new LoggerConfiguration()
2324
.WriteTo.Console(new ExpressionTemplate(
24-
"[{@t:HH:mm:ss} " +
25-
"{@l:u3} " +
26-
"({coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')})] " +
27-
"{@m} " +
28-
"(first item is {coalesce(Items[0], '<empty>')})" +
29-
"\n" +
30-
"{@x}"))
25+
"[{@t:HH:mm:ss} {@l:u3}" +
26+
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
27+
"{@m} (first item is {coalesce(Items[0], '<empty>')})\n{@x}"))
3128
.CreateLogger();
3229

33-
log.Information("Running {Example}", nameof(TextFormattingExample));
30+
log.Information("Running {Example}", nameof(TextFormattingExample1));
3431

3532
log.ForContext<Program>()
3633
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
@@ -75,5 +72,26 @@ static void PipelineComponentExample()
7572
log.ForContext<Program>()
7673
.Information("Cart contains {@Items}", new[] { "Apricots" });
7774
}
75+
76+
static void TextFormattingExample2()
77+
{
78+
using var log = new LoggerConfiguration()
79+
.WriteTo.Console(new ExpressionTemplate(
80+
"{@l:w4}: {SourceContext}\n" +
81+
"{#if Scope is not null}" +
82+
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
83+
"{#end}" +
84+
" {@m}\n" +
85+
"{@x}"))
86+
.CreateLogger();
87+
88+
var program = log.ForContext<Program>();
89+
program.Information("Starting up");
90+
91+
// Emulate data produced by the Serilog.AspNetCore integration
92+
var scoped = program.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"});
93+
94+
scoped.Information("Hello, world!");
95+
}
7896
}
7997
}

src/Serilog.Expressions/Templates/Compilation/NameResolution/TemplateLocalNameResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ Template Transform(FormattedExpression fx, Stack<string> locals)
4949
Template Transform(Conditional cond, Stack<string> locals)
5050
{
5151
return new Conditional(
52-
cond.Condition,
53-
cond.Consequent,
52+
ExpressionLocalNameBinder.BindLocalValueNames(cond.Condition, locals),
53+
Transform(cond.Consequent, locals),
5454
cond.Alternative != null ? Transform(cond.Alternative, locals) : null);
5555
}
5656

0 commit comments

Comments
 (0)