1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using Serilog ;
3
4
using Serilog . Debugging ;
4
5
using Serilog . Templates ;
6
+ using Serilog . Templates . Themes ;
5
7
6
8
namespace Sample
7
9
{
@@ -24,7 +26,8 @@ static void TextFormattingExample1()
24
26
. WriteTo . Console ( new ExpressionTemplate (
25
27
"[{@t:HH:mm:ss} {@l:u3}" +
26
28
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
27
- "{@m} (first item is {coalesce(Items[0], '<empty>')})\n {@x}" ) )
29
+ "{@m} (first item is {coalesce(Items[0], '<empty>')})\n {@x}" ,
30
+ theme : TemplateTheme . Code ) )
28
31
. CreateLogger ( ) ;
29
32
30
33
log . Information ( "Running {Example}" , nameof ( TextFormattingExample1 ) ) ;
@@ -41,7 +44,7 @@ static void JsonFormattingExample()
41
44
using var log = new LoggerConfiguration ( )
42
45
. Enrich . WithProperty ( "Application" , "Example" )
43
46
. WriteTo . Console ( new ExpressionTemplate (
44
- "{ {@t, @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n " ) )
47
+ "{ {@t: UtcDateTime(@t) , @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n " ) )
45
48
. CreateLogger ( ) ;
46
49
47
50
log . Information ( "Running {Example}" , nameof ( JsonFormattingExample ) ) ;
@@ -75,23 +78,35 @@ static void PipelineComponentExample()
75
78
76
79
static void TextFormattingExample2 ( )
77
80
{
81
+ // Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
82
+
83
+ var melon = new TemplateTheme ( TemplateTheme . Literate , new Dictionary < TemplateThemeStyle , string >
84
+ {
85
+ // `Information` is dark green in MEL.
86
+ [ TemplateThemeStyle . LevelInformation ] = "\x1b [38;5;34m" ,
87
+ [ TemplateThemeStyle . String ] = "\x1b [38;5;159m" ,
88
+ [ TemplateThemeStyle . Number ] = "\x1b [38;5;159m"
89
+ } ) ;
90
+
78
91
using var log = new LoggerConfiguration ( )
79
92
. WriteTo . Console ( new ExpressionTemplate (
80
93
"{@l:w4}: {SourceContext}\n " +
81
94
"{#if Scope is not null}" +
82
95
" {#each s in Scope}=> {s}{#delimit} {#end}\n " +
83
96
"{#end}" +
84
97
" {@m}\n " +
85
- "{@x}" ) )
98
+ "{@x}" ,
99
+ theme : melon ) )
86
100
. CreateLogger ( ) ;
87
101
88
102
var program = log . ForContext < Program > ( ) ;
89
- program . Information ( "Starting up " ) ;
103
+ program . Information ( "Host listening at {ListenUri}" , "https://hello-world.local ") ;
90
104
91
- // Emulate data produced by the Serilog.AspNetCore integration
92
- var scoped = program . ForContext ( "Scope" , new [ ] { "Main" , "TextFormattingExample2()" } ) ;
105
+ program
106
+ . ForContext ( "Scope" , new [ ] { "Main" , "TextFormattingExample2()" } )
107
+ . Information ( "HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms" , "GET" , "/api/hello" , 200 , 1.23 ) ;
93
108
94
- scoped . Information ( "Hello, world! ") ;
109
+ program . Warning ( "We've reached the end of the line ") ;
95
110
}
96
111
}
97
112
}
0 commit comments