Skip to content

Commit d9e865f

Browse files
committed
Sample updates
1 parent 2682597 commit d9e865f

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ calling a function will be undefined if:
171171
| `TypeOf(x)` | Returns a string describing the type of expression `x`: a .NET type name if `x` is scalar and non-null, or, `'array'`, `'object'`, `'dictionary'`, `'null'`, or `'undefined'`. |
172172
| `Undefined()` | Explicitly mark an undefined value. |
173173

174-
Functions that compare text accept an optional post-fix `ci` modifier to select case-insensitive comparisons:
174+
Functions that compare text accept an optional postfix `ci` modifier to select case-insensitive comparisons:
175175

176176
```
177177
StartsWith(User.Name, 'n') ci

example/Sample/Program.cs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,69 @@ public class Program
1111
public static void Main()
1212
{
1313
SelfLog.Enable(Console.Error);
14+
15+
TextFormattingExample();
16+
JsonFormattingExample();
17+
PipelineComponentExample();
18+
}
19+
20+
static void TextFormattingExample()
21+
{
22+
using var log = new LoggerConfiguration()
23+
.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}"))
31+
.CreateLogger();
32+
33+
log.Information("Running {Example}", nameof(TextFormattingExample));
34+
35+
log.ForContext<Program>()
36+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
37+
38+
log.ForContext<Program>()
39+
.Information("Cart contains {@Items}", new[] { "Apricots" });
40+
}
41+
42+
static void JsonFormattingExample()
43+
{
44+
using var log = new LoggerConfiguration()
45+
.Enrich.WithProperty("Application", "Example")
46+
.WriteTo.Console(new ExpressionTemplate(
47+
"{ {@t, @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
48+
.CreateLogger();
49+
50+
log.Information("Running {Example}", nameof(JsonFormattingExample));
1451

52+
log.ForContext<Program>()
53+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
54+
55+
log.ForContext<Program>()
56+
.Warning("Cart is empty");
57+
}
58+
59+
static void PipelineComponentExample()
60+
{
1561
using var log = new LoggerConfiguration()
16-
.Enrich.WithProperty("AppId", 10)
17-
.Enrich.WithComputed("FirstItem", "Items[0]")
62+
.Enrich.WithProperty("Application", "Example")
63+
.Enrich.WithComputed("FirstItem", "coalesce(Items[0], '<empty>')")
1864
.Enrich.WithComputed("SourceContext", "coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')")
19-
.Filter.ByIncludingOnly("@l = 'Information' and AppId is not null and Items[?] like 'C%'")
65+
.Filter.ByIncludingOnly("Items is null or Items[?] like 'C%'")
2066
.WriteTo.Console(outputTemplate:
2167
"[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}")
22-
.WriteTo.Console(new ExpressionTemplate(
23-
"[{@t:HH:mm:ss} {@l:u3} ({SourceContext})] {@m} (first item is {Items[0]})\n{@x}"))
24-
.CreateLogger();
25-
26-
log.ForContext<Program>().Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
27-
log.Warning("Cart contains {@Items}", new[] { "Tea", "Coffee" });
28-
log.Information("Cart contains {@Items}", new[] { "Apricots" });
29-
log.Information("Cart for {Name} contains {@Items}", Environment.UserName, new[] { "Peanuts", "Chocolate" });
68+
.CreateLogger();
69+
70+
log.Information("Running {Example}", nameof(PipelineComponentExample));
71+
72+
log.ForContext<Program>()
73+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
74+
75+
log.ForContext<Program>()
76+
.Information("Cart contains {@Items}", new[] { "Apricots" });
3077
}
3178
}
3279
}

0 commit comments

Comments
 (0)