Skip to content

Commit bc36f47

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 bc36f47

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
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/).

Thinktecture.Extensions.Logging.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ Global
4848
HideSolutionNode = FALSE
4949
EndGlobalSection
5050
GlobalSection(NestedProjects) = preSolution
51-
{91187C47-B86C-4E6B-8812-F27C8C177842} = {F2EF52EE-D542-49B4-A80B-6DFE4BE457C9}
5251
{2CBCA633-63D3-4B45-8773-9D1A60FB1D51} = {815137E4-6499-47FA-8191-C646BC474B54}
5352
{0756A7B3-BFC2-4C1C-BFAC-69CC6A036D99} = {34773EDF-B53E-49D6-8586-93D838AE023F}
5453
{F92AE982-743E-435C-80F9-A74A3A289844} = {F2EF52EE-D542-49B4-A80B-6DFE4BE457C9}
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
}

examples/Thinktecture.Extensions.Logging.Abstractions.Example/Thinktecture.Extensions.Logging.Abstractions.Example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

tests/Thinktecture.Extensions.Logging.Abstractions.Tests/Thinktecture.Extensions.Logging.Abstractions.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.0</TargetFramework>
3+
<TargetFramework>netcoreapp2.1</TargetFramework>
44
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
55
<RootNamespace>Thinktecture</RootNamespace>
66
</PropertyGroup>

0 commit comments

Comments
 (0)