Skip to content

Commit 9bae363

Browse files
committed
Merge pull request #37 from nblumhardt/dev
Efficiently construct Serilog events using BindProperty()
2 parents 009cafd + 9b3662d commit 9bae363

File tree

4 files changed

+38
-64
lines changed

4 files changed

+38
-64
lines changed

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ image: Visual Studio 2015
33
configuration: Release
44
install:
55
- ps: mkdir -Force ".\build\" | Out-Null
6-
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/install.ps1" -OutFile ".\build\installcli.ps1"
6+
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
77
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
8-
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath'
8+
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 1.0.0-preview2-002823'
99
- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
1010
build_script:
1111
- ps: ./Build.ps1

src/Serilog.Extensions.Logging/Extensions/Logging/EventIdEnricher.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Extensions.Logging;
55
using System;
66
using System.Collections.Generic;
7-
using System.Linq;
87
using Serilog.Core;
98
using Serilog.Events;
109
using FrameworkLogger = Microsoft.Extensions.Logging.ILogger;
@@ -59,6 +58,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
5958
var logger = _logger;
6059
string messageTemplate = null;
6160

61+
var properties = new List<LogEventProperty>();
62+
6263
var structure = state as IEnumerable<KeyValuePair<string, object>>;
6364
if (structure != null)
6465
{
@@ -70,11 +71,15 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
7071
}
7172
else if (property.Key.StartsWith("@"))
7273
{
73-
logger = logger.ForContext(property.Key.Substring(1), property.Value, destructureObjects: true);
74+
LogEventProperty destructured;
75+
if (logger.BindProperty(property.Key.Substring(1), property.Value, true, out destructured))
76+
properties.Add(destructured);
7477
}
7578
else
7679
{
77-
logger = logger.ForContext(property.Key, property.Value);
80+
LogEventProperty bound;
81+
if (logger.BindProperty(property.Key, property.Value, false, out bound))
82+
properties.Add(bound);
7883
}
7984
}
8085

@@ -84,24 +89,28 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
8489
if (messageTemplate == null && !stateTypeInfo.IsGenericType)
8590
{
8691
messageTemplate = "{" + stateType.Name + ":l}";
87-
logger = logger.ForContext(stateType.Name, AsLoggableValue(state, formatter));
92+
LogEventProperty stateTypeProperty;
93+
if (logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out stateTypeProperty))
94+
properties.Add(stateTypeProperty);
8895
}
8996
}
9097

9198
if (messageTemplate == null && state != null)
9299
{
93100
messageTemplate = "{State:l}";
94-
logger = logger.ForContext("State", AsLoggableValue(state, formatter));
101+
LogEventProperty stateProperty;
102+
if (logger.BindProperty("State", AsLoggableValue(state, formatter), false, out stateProperty))
103+
properties.Add(stateProperty);
95104
}
96105

97106
if (string.IsNullOrEmpty(messageTemplate))
98107
return;
99108

100109
if (eventId.Id != 0 || eventId.Name != null)
101-
logger = logger.ForContext(new[] { new EventIdEnricher(eventId) });
110+
properties.Add(CreateEventIdProperty(eventId));
102111

103112
var parsedTemplate = _messageTemplateParser.Parse(messageTemplate);
104-
var evt = new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, Enumerable.Empty<LogEventProperty>());
113+
var evt = new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);
105114
logger.Write(evt);
106115
}
107116

@@ -133,5 +142,22 @@ static LogEventLevel ConvertLevel(LogLevel logLevel)
133142
return LogEventLevel.Verbose;
134143
}
135144
}
145+
146+
static LogEventProperty CreateEventIdProperty(EventId eventId)
147+
{
148+
var properties = new List<LogEventProperty>(2);
149+
150+
if (eventId.Id != 0)
151+
{
152+
properties.Add(new LogEventProperty("Id", new ScalarValue(eventId.Id)));
153+
}
154+
155+
if (eventId.Name != null)
156+
{
157+
properties.Add(new LogEventProperty("Name", new ScalarValue(eventId.Name)));
158+
}
159+
160+
return new LogEventProperty("EventId", new StructureValue(properties));
161+
}
136162
}
137163
}

src/Serilog.Extensions.Logging/project.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"version": "1.0.0-rc2-*",
33
"description": "Serilog provider for Microsoft.Extensions.Logging",
44
"authors": [ "Microsoft", "Serilog Contributors" ],
@@ -9,8 +9,8 @@
99
"iconUrl": "http://serilog.net/images/serilog-extension-nuget.png"
1010
},
1111
"dependencies": {
12-
"Serilog": "2.0.0-rc-556",
13-
"Microsoft.Extensions.Logging": "1.0.0-rc2-final"
12+
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
13+
"Serilog": "2.0.0-rc-576"
1414
},
1515
"buildOptions": {
1616
"keyFile": "../../assets/Serilog.snk",

0 commit comments

Comments
 (0)