@@ -12,25 +12,22 @@ public static void Main()
12
12
{
13
13
SelfLog . Enable ( Console . Error ) ;
14
14
15
- TextFormattingExample ( ) ;
15
+ TextFormattingExample1 ( ) ;
16
16
JsonFormattingExample ( ) ;
17
17
PipelineComponentExample ( ) ;
18
+ TextFormattingExample2 ( ) ;
18
19
}
19
20
20
- static void TextFormattingExample ( )
21
+ static void TextFormattingExample1 ( )
21
22
{
22
23
using var log = new LoggerConfiguration ( )
23
24
. 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}" ) )
31
28
. CreateLogger ( ) ;
32
29
33
- log . Information ( "Running {Example}" , nameof ( TextFormattingExample ) ) ;
30
+ log . Information ( "Running {Example}" , nameof ( TextFormattingExample1 ) ) ;
34
31
35
32
log . ForContext < Program > ( )
36
33
. Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
@@ -75,5 +72,26 @@ static void PipelineComponentExample()
75
72
log . ForContext < Program > ( )
76
73
. Information ( "Cart contains {@Items}" , new [ ] { "Apricots" } ) ;
77
74
}
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
+ }
78
96
}
79
97
}
0 commit comments