Skip to content

Commit 461a287

Browse files
authored
Merge pull request #12 from serilog/dev
3.1.0 Release
2 parents 616d521 + e26ccd0 commit 461a287

10 files changed

+138
-66
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,31 @@ Log.Logger = new LoggerConfiguration()
1717
.Enrich.WithThreadId()
1818
.CreateLogger();
1919
```
20-
20+
Many sinks simply include all properties without further action required, so the thread id will be logged automatically.
21+
However, some sinks, such as the File and Console sinks use an output template and the new ThreadId may not be automatically output in your sink. In this case, in order for the ThreadId to show up in the logging, you will need to create or modify your output template.
22+
23+
```csharp
24+
w.File(...., outputTemplate:
25+
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}")
26+
```
27+
Here, {Properties} can include not only ThreadId, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment.
28+
29+
An example, which also uses the Serilogs.Sinks.Async Nuget package, is below:
30+
31+
```csharp
32+
var logger = Log.Logger = new LoggerConfiguration()
33+
.MinimumLevel.Debug()
34+
.WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)
35+
.WriteTo.Async(w=>w.File("..\\..\\..\\..\\logs\\SerilogLogFile.json", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} <{ThreadId}>{NewLine}{Exception}"))
36+
.Enrich.WithThreadId()
37+
.CreateLogger();
38+
```
39+
Which would produce an output in the log file as follows:
40+
```
41+
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4>
42+
```
43+
Where, <4> is an example thread id.
44+
2145
To use the enricher, first install the NuGet package:
2246

2347
```powershell

appveyor.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2015
3+
image: Visual Studio 2017
44
configuration: Release
5-
install:
6-
- ps: mkdir -Force ".\build\" | Out-Null
7-
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
8-
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
9-
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 1.0.0-preview2-003121'
10-
- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
115
build_script:
126
- ps: ./Build.ps1
137
test: off
@@ -16,7 +10,7 @@ artifacts:
1610
deploy:
1711
- provider: NuGet
1812
api_key:
19-
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
13+
secure: N59tiJECUYpip6tEn0xvdmDAEiP9SIzyLEFLpwiigm/8WhJvBNs13QxzT1/3/JW/
2014
skip_symbols: true
2115
on:
2216
branch: /^(master|dev)$/

global.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"projects": [ "src", "test" ],
3-
"sdk": {
4-
"version": "1.0.0-preview1-002702"
5-
}
6-
}
1+
{"projects":["src","test"]}

serilog-enrichers-thread.sln

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.329
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
77
EndProject
@@ -14,7 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5
1414
assets\Serilog.snk = assets\Serilog.snk
1515
EndProjectSection
1616
EndProject
17-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Enrichers.Thread", "src\Serilog.Enrichers.Thread\Serilog.Enrichers.Thread.xproj", "{E0BD3797-55A5-4E8C-8DE7-8487BEB75914}"
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Thread", "src\Serilog.Enrichers.Thread\Serilog.Enrichers.Thread.csproj", "{E0BD3797-55A5-4E8C-8DE7-8487BEB75914}"
1818
EndProject
1919
Global
2020
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -33,4 +33,7 @@ Global
3333
GlobalSection(NestedProjects) = preSolution
3434
{E0BD3797-55A5-4E8C-8DE7-8487BEB75914} = {037440DE-440B-4129-9F7A-09B42D00397E}
3535
EndGlobalSection
36+
GlobalSection(ExtensibilityGlobals) = postSolution
37+
SolutionGuid = {155C418E-39FB-4037-B825-7FEE96CEBDFB}
38+
EndGlobalSection
3639
EndGlobal

src/Serilog.Enrichers.Thread/Enrichers/ThreadIdEnricher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2015 Serilog Contributors
1+
// Copyright 2013-2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013-2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if THREAD_NAME
16+
using System.Threading;
17+
using Serilog.Core;
18+
using Serilog.Events;
19+
20+
namespace Serilog.Enrichers
21+
{
22+
23+
/// <summary>
24+
/// Enriches log events with a ThreadName property containing the
25+
/// </summary>
26+
public class ThreadNameEnricher : ILogEventEnricher
27+
{
28+
/// <summary>
29+
/// The property name added to enriched log events.
30+
/// </summary>
31+
public const string ThreadNamePropertyName = "ThreadName";
32+
33+
/// <summary>
34+
/// Enrich the log event.
35+
/// </summary>
36+
/// <param name="logEvent">The log event to enrich.</param>
37+
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
38+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
39+
{
40+
var threadName = Thread.CurrentThread.Name;
41+
if (threadName != null)
42+
{
43+
logEvent.AddPropertyIfAbsent(new LogEventProperty(ThreadNamePropertyName, new ScalarValue(threadName)));
44+
}
45+
}
46+
}
47+
}
48+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>Enrich Serilog events with properties from the current thread.</Description>
5+
<VersionPrefix>3.1.0</VersionPrefix>
6+
<Authors>Serilog Contributors</Authors>
7+
<TargetFrameworks>net45;netstandard1.0;netstandard2.0</TargetFrameworks>
8+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10+
<AssemblyName>Serilog.Enrichers.Thread</AssemblyName>
11+
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
12+
<SignAssembly>true</SignAssembly>
13+
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
14+
<PackageId>Serilog.Enrichers.Thread</PackageId>
15+
<PackageTags>serilog;thread;enricher</PackageTags>
16+
<PackageIconUrl>http://serilog.net/images/serilog-enricher-nuget.png</PackageIconUrl>
17+
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
18+
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
19+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Serilog" Version="2.3.0" />
24+
</ItemGroup>
25+
26+
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
27+
<Reference Include="System" />
28+
<Reference Include="Microsoft.CSharp" />
29+
</ItemGroup>
30+
31+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
32+
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
33+
</PropertyGroup>
34+
35+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
36+
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
37+
</PropertyGroup>
38+
39+
</Project>

src/Serilog.Enrichers.Thread/Serilog.Enrichers.Thread.xproj

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

src/Serilog.Enrichers.Thread/ThreadLoggerConfigurationExtensions.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2016 Serilog Contributors
1+
// Copyright 2013-2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -38,5 +38,19 @@ public static LoggerConfiguration WithThreadId(
3838
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
3939
return enrichmentConfiguration.With<ThreadIdEnricher>();
4040
}
41+
42+
#if THREAD_NAME
43+
/// <summary>
44+
/// Enrich log events with a ThreadName property containing the <see cref="Thread.CurrentThread"/> <see cref="Thread.Name"/>.
45+
/// </summary>
46+
/// <param name="enrichmentConfiguration"></param>
47+
/// <returns></returns>
48+
public static LoggerConfiguration WithThreadName(
49+
this LoggerEnrichmentConfiguration enrichmentConfiguration)
50+
{
51+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
52+
return enrichmentConfiguration.With<ThreadNameEnricher>();
53+
}
54+
#endif
4155
}
42-
}
56+
}

src/Serilog.Enrichers.Thread/project.json

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

0 commit comments

Comments
 (0)