Skip to content

Commit 1e9f1c8

Browse files
committed
Updated example to make it clear that structured logging is not possible with current compiler (dotnet/roslyn#142).
1 parent 4203596 commit 1e9f1c8

File tree

1 file changed

+25
-4
lines changed
  • examples/Thinktecture.Extensions.Logging.Abstractions.Example

1 file changed

+25
-4
lines changed
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Linq;
23
using Microsoft.Extensions.Logging;
34
using Serilog;
5+
using Serilog.Core;
6+
using Serilog.Events;
47

58
namespace Thinktecture.Extensions.Logging.Abstractions.Example
69
{
@@ -9,11 +12,12 @@ class Program
912
static void Main(string[] args)
1013
{
1114
var serilog = new LoggerConfiguration()
12-
.WriteTo.Console()
13-
.CreateLogger();
15+
.WriteTo.Console()
16+
.WriteTo.Sink(new MySink())
17+
.CreateLogger();
1418
var logger = new LoggerFactory()
15-
.AddSerilog(serilog)
16-
.CreateLogger<Program>();
19+
.AddSerilog(serilog)
20+
.CreateLogger<Program>();
1721

1822
var a = "text";
1923
var b = 42;
@@ -24,4 +28,21 @@ static void Main(string[] args)
2428
Console.ReadLine();
2529
}
2630
}
31+
32+
internal class MySink : ILogEventSink
33+
{
34+
public void Emit(LogEvent logEvent)
35+
{
36+
Console.WriteLine();
37+
Console.WriteLine($"Template: {logEvent.MessageTemplate.Text}");
38+
Console.WriteLine("Properties:");
39+
40+
foreach (var kvp in logEvent.Properties)
41+
{
42+
// Issue: formattable string does not capture the names of the placeholders
43+
// https://github.com/dotnet/roslyn/issues/142
44+
Console.WriteLine($"\t{kvp.Key} = {kvp.Value}");
45+
}
46+
}
47+
}
2748
}

0 commit comments

Comments
 (0)