1- using System ;
2- using System . Collections . Generic ;
3- using Serilog ;
1+ using Serilog ;
42using Serilog . Debugging ;
53using Serilog . Templates ;
64using Serilog . Templates . Themes ;
75
8- namespace Sample
6+ namespace Sample ;
7+
8+ // ReSharper disable once ClassNeverInstantiated.Global
9+ public class Program
910{
10- // ReSharper disable once ClassNeverInstantiated.Global
11- public class Program
11+ public static void Main ( )
1212 {
13- public static void Main ( )
14- {
15- SelfLog . Enable ( Console . Error ) ;
16-
17- TextFormattingExample1 ( ) ;
18- JsonFormattingExample ( ) ;
19- PipelineComponentExample ( ) ;
20- TextFormattingExample2 ( ) ;
21- }
22-
23- static void TextFormattingExample1 ( )
24- {
25- using var log = new LoggerConfiguration ( )
26- . Enrich . WithProperty ( "Application" , "Sample" )
27- . WriteTo . Console ( new ExpressionTemplate (
28- "[{@t:HH:mm:ss} {@l:u3}" +
29- "{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
30- "{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n {@x}" ,
31- theme : TemplateTheme . Code ) )
32- . CreateLogger ( ) ;
13+ SelfLog . Enable ( Console . Error ) ;
3314
34- log . Information ( "Running {Example}" , nameof ( TextFormattingExample1 ) ) ;
35-
36- log . ForContext < Program > ( )
37- . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
38-
39- log . ForContext < Program > ( )
40- . Information ( "Cart contains {@Items}" , new [ ] { "Apricots" } ) ;
41- }
42-
43- static void JsonFormattingExample ( )
44- {
45- using var log = new LoggerConfiguration ( )
46- . Enrich . WithProperty ( "Application" , "Example" )
47- . WriteTo . Console ( new ExpressionTemplate (
48- "{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n " ) )
49- . CreateLogger ( ) ;
15+ TextFormattingExample1 ( ) ;
16+ JsonFormattingExample ( ) ;
17+ PipelineComponentExample ( ) ;
18+ TextFormattingExample2 ( ) ;
19+ }
5020
51- log . Information ( "Running {Example}" , nameof ( JsonFormattingExample ) ) ;
21+ static void TextFormattingExample1 ( )
22+ {
23+ using var log = new LoggerConfiguration ( )
24+ . Enrich . WithProperty ( "Application" , "Sample" )
25+ . WriteTo . Console ( new ExpressionTemplate (
26+ "[{@t:HH:mm:ss} {@l:u3}" +
27+ "{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
28+ "{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n {@x}" ,
29+ theme : TemplateTheme . Code ) )
30+ . CreateLogger ( ) ;
31+
32+ log . Information ( "Running {Example}" , nameof ( TextFormattingExample1 ) ) ;
33+
34+ log . ForContext < Program > ( )
35+ . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
36+
37+ log . ForContext < Program > ( )
38+ . Information ( "Cart contains {@Items}" , new [ ] { "Apricots" } ) ;
39+ }
5240
53- log . ForContext < Program > ( )
54- . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
41+ static void JsonFormattingExample ( )
42+ {
43+ using var log = new LoggerConfiguration ( )
44+ . Enrich . WithProperty ( "Application" , "Example" )
45+ . WriteTo . Console ( new ExpressionTemplate (
46+ "{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n " ) )
47+ . CreateLogger ( ) ;
5548
56- log . ForContext < Program > ( )
57- . Warning ( "Cart is empty" ) ;
58- }
49+ log . Information ( "Running {Example}" , nameof ( JsonFormattingExample ) ) ;
5950
60- static void PipelineComponentExample ( )
61- {
62- using var log = new LoggerConfiguration ( )
63- . Enrich . WithProperty ( "Application" , "Example" )
64- . Enrich . WithComputed ( "FirstItem" , "coalesce(Items[0], '<empty>')" )
65- . Enrich . WithComputed ( "SourceContext" , "coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')" )
66- . Filter . ByIncludingOnly ( "Items is null or Items[?] like 'C%'" )
67- . WriteTo . Console ( outputTemplate :
68- "[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}" )
69- . CreateLogger ( ) ;
51+ log . ForContext < Program > ( )
52+ . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
7053
71- log . Information ( "Running {Example}" , nameof ( PipelineComponentExample ) ) ;
54+ log . ForContext < Program > ( )
55+ . Warning ( "Cart is empty" ) ;
56+ }
7257
73- log . ForContext < Program > ( )
74- . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
58+ static void PipelineComponentExample ( )
59+ {
60+ using var log = new LoggerConfiguration ( )
61+ . Enrich . WithProperty ( "Application" , "Example" )
62+ . Enrich . WithComputed ( "FirstItem" , "coalesce(Items[0], '<empty>')" )
63+ . Enrich . WithComputed ( "SourceContext" , "coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')" )
64+ . Filter . ByIncludingOnly ( "Items is null or Items[?] like 'C%'" )
65+ . WriteTo . Console ( outputTemplate :
66+ "[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}" )
67+ . CreateLogger ( ) ;
68+
69+ log . Information ( "Running {Example}" , nameof ( PipelineComponentExample ) ) ;
70+
71+ log . ForContext < Program > ( )
72+ . Information ( "Cart contains {@Items}" , new [ ] { "Tea" , "Coffee" } ) ;
73+
74+ log . ForContext < Program > ( )
75+ . Information ( "Cart contains {@Items}" , new [ ] { "Apricots" } ) ;
76+ }
7577
76- log . ForContext < Program > ( )
77- . Information ( "Cart contains {@Items}" , new [ ] { "Apricots" } ) ;
78- }
78+ static void TextFormattingExample2 ( )
79+ {
80+ // Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
7981
80- static void TextFormattingExample2 ( )
82+ var melon = new TemplateTheme ( TemplateTheme . Literate , new Dictionary < TemplateThemeStyle , string >
8183 {
82- // Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
83-
84- var melon = new TemplateTheme ( TemplateTheme . Literate , new Dictionary < TemplateThemeStyle , string >
85- {
86- // `Information` is dark green in MEL.
87- [ TemplateThemeStyle . LevelInformation ] = "\x1b [38;5;34m" ,
88- [ TemplateThemeStyle . String ] = "\x1b [38;5;159m" ,
89- [ TemplateThemeStyle . Number ] = "\x1b [38;5;159m"
90- } ) ;
91-
92- using var log = new LoggerConfiguration ( )
93- . WriteTo . Console ( new ExpressionTemplate (
94- "{@l:w4}: {SourceContext}\n " +
95- "{#if Scope is not null}" +
96- " {#each s in Scope}=> {s}{#delimit} {#end}\n " +
97- "{#end}" +
98- " {@m}\n " +
99- "{@x}" ,
100- theme : melon ) )
101- . CreateLogger ( ) ;
102-
103- var program = log . ForContext < Program > ( ) ;
104- program . Information ( "Host listening at {ListenUri}" , "https://hello-world.local" ) ;
105-
106- program
107- . ForContext ( "Scope" , new [ ] { "Main" , "TextFormattingExample2()" } )
108- . Information ( "HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms" , "GET" , "/api/hello" , 200 , 1.23 ) ;
109-
110- program . Warning ( "We've reached the end of the line" ) ;
111- }
84+ // `Information` is dark green in MEL.
85+ [ TemplateThemeStyle . LevelInformation ] = "\x1b [38;5;34m" ,
86+ [ TemplateThemeStyle . String ] = "\x1b [38;5;159m" ,
87+ [ TemplateThemeStyle . Number ] = "\x1b [38;5;159m"
88+ } ) ;
89+
90+ using var log = new LoggerConfiguration ( )
91+ . WriteTo . Console ( new ExpressionTemplate (
92+ "{@l:w4}: {SourceContext}\n " +
93+ "{#if Scope is not null}" +
94+ " {#each s in Scope}=> {s}{#delimit} {#end}\n " +
95+ "{#end}" +
96+ " {@m}\n " +
97+ "{@x}" ,
98+ theme : melon ) )
99+ . CreateLogger ( ) ;
100+
101+ var program = log . ForContext < Program > ( ) ;
102+ program . Information ( "Host listening at {ListenUri}" , "https://hello-world.local" ) ;
103+
104+ program
105+ . ForContext ( "Scope" , new [ ] { "Main" , "TextFormattingExample2()" } )
106+ . Information ( "HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms" , "GET" , "/api/hello" , 200 , 1.23 ) ;
107+
108+ program . Warning ( "We've reached the end of the line" ) ;
112109 }
113- }
110+ }
0 commit comments