Skip to content

Commit

Permalink
Updated example to make it clear that structured logging is not possi…
Browse files Browse the repository at this point in the history
…ble with current compiler (dotnet/roslyn#142).
  • Loading branch information
PawelGerr committed Oct 8, 2018
1 parent 5bd2254 commit bc36f47
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[![Build status](https://ci.appveyor.com/api/projects/status/ys8hswkylkqbf734?svg=true)](https://ci.appveyor.com/project/PawelGerr/thinktecture-extensions-logging)

[![Build status](https://ci.appveyor.com/api/projects/status/ys8hswkylkqbf734?svg=true)](https://ci.appveyor.com/project/PawelGerr/thinktecture-extensions-logging)

Adds string interpolation support to [ILogger](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Abstractions/).
1 change: 0 additions & 1 deletion Thinktecture.Extensions.Logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{91187C47-B86C-4E6B-8812-F27C8C177842} = {F2EF52EE-D542-49B4-A80B-6DFE4BE457C9}
{2CBCA633-63D3-4B45-8773-9D1A60FB1D51} = {815137E4-6499-47FA-8191-C646BC474B54}
{0756A7B3-BFC2-4C1C-BFAC-69CC6A036D99} = {34773EDF-B53E-49D6-8586-93D838AE023F}
{F92AE982-743E-435C-80F9-A74A3A289844} = {F2EF52EE-D542-49B4-A80B-6DFE4BE457C9}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
using System;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace Thinktecture.Extensions.Logging.Abstractions.Example
{
class Program
{
static void Main(string[] args)
{
var serilog = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var logger = new LoggerFactory()
.AddSerilog(serilog)
.CreateLogger<Program>();
class Program
{
static void Main(string[] args)
{
var serilog = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.Sink(new MySink())
.CreateLogger();
var logger = new LoggerFactory()
.AddSerilog(serilog)
.CreateLogger<Program>();

var a = "text";
var b = 42;
logger.LogInformation((string)$"Using standard extension method: a = {a}, b = {b}");
logger.LogInformation($"Using new extension method: a = {a}, b = {b}");
var a = "text";
var b = 42;
logger.LogInformation((string)$"Using standard extension method: a = {a}, b = {b}");
logger.LogInformation($"Using new extension method: a = {a}, b = {b}");

Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
}
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
}

internal class MySink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
Console.WriteLine();
Console.WriteLine($"Template: {logEvent.MessageTemplate.Text}");
Console.WriteLine("Properties:");

foreach (var kvp in logEvent.Properties)
{
// Issue: formattable string does not capture the names of the placeholders
// https://github.com/dotnet/roslyn/issues/142
Console.WriteLine($"\t{kvp.Key} = {kvp.Value}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RootNamespace>Thinktecture</RootNamespace>
</PropertyGroup>
Expand Down

0 comments on commit bc36f47

Please sign in to comment.