Skip to content

Commit d7c2242

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

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
[![Build status](https://ci.appveyor.com/api/projects/status/ys8hswkylkqbf734?svg=true)](https://ci.appveyor.com/project/PawelGerr/thinktecture-extensions-logging)
2-
1+
[![Build status](https://ci.appveyor.com/api/projects/status/ys8hswkylkqbf734?svg=true)](https://ci.appveyor.com/project/PawelGerr/thinktecture-extensions-logging)
2+
3+
Adds string interpolation support to [ILogger](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Abstractions/).
Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
using System;
22
using Microsoft.Extensions.Logging;
33
using Serilog;
4+
using Serilog.Core;
5+
using Serilog.Events;
46

57
namespace Thinktecture.Extensions.Logging.Abstractions.Example
68
{
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>();
1720

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}");
2225

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+
}
2747
}

0 commit comments

Comments
 (0)