|
1 | 1 | using System;
|
2 | 2 | using Microsoft.Extensions.Logging;
|
3 | 3 | using Serilog;
|
| 4 | +using Serilog.Core; |
| 5 | +using Serilog.Events; |
4 | 6 |
|
5 | 7 | namespace Thinktecture.Extensions.Logging.Abstractions.Example
|
6 | 8 | {
|
7 |
| - class Program |
8 |
| - { |
9 |
| - static void Main(string[] args) |
10 |
| - { |
11 |
| - var serilog = new LoggerConfiguration() |
12 |
| - .WriteTo.Console() |
13 |
| - .CreateLogger(); |
14 |
| - var logger = new LoggerFactory() |
15 |
| - .AddSerilog(serilog) |
16 |
| - .CreateLogger<Program>(); |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + var serilog = new LoggerConfiguration() |
| 14 | + .WriteTo.Console() |
| 15 | + .WriteTo.Sink(new MySink()) |
| 16 | + .CreateLogger(); |
| 17 | + var logger = new LoggerFactory() |
| 18 | + .AddSerilog(serilog) |
| 19 | + .CreateLogger<Program>(); |
17 | 20 |
|
18 |
| - var a = "text"; |
19 |
| - var b = 42; |
20 |
| - logger.LogInformation((string)$"Using standard extension method: a = {a}, b = {b}"); |
21 |
| - logger.LogInformation($"Using new extension method: a = {a}, b = {b}"); |
| 21 | + var a = "text"; |
| 22 | + var b = 42; |
| 23 | + logger.LogInformation((string)$"Using standard extension method: a = {a}, b = {b}"); |
| 24 | + logger.LogInformation($"Using new extension method: a = {a}, b = {b}"); |
22 | 25 |
|
23 |
| - Console.WriteLine("Press ENTER to exit."); |
24 |
| - Console.ReadLine(); |
25 |
| - } |
26 |
| - } |
| 26 | + Console.WriteLine("Press ENTER to exit."); |
| 27 | + Console.ReadLine(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + internal class MySink : ILogEventSink |
| 32 | + { |
| 33 | + public void Emit(LogEvent logEvent) |
| 34 | + { |
| 35 | + Console.WriteLine(); |
| 36 | + Console.WriteLine($"Template: {logEvent.MessageTemplate.Text}"); |
| 37 | + Console.WriteLine("Properties:"); |
| 38 | + |
| 39 | + foreach (var kvp in logEvent.Properties) |
| 40 | + { |
| 41 | + // Issue: formattable string does not capture the names of the placeholders |
| 42 | + // https://github.com/dotnet/roslyn/issues/142 |
| 43 | + Console.WriteLine($"\t{kvp.Key} = {kvp.Value}"); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
27 | 47 | }
|
0 commit comments