Skip to content

Add simple caching into enrichers #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ Log.Logger = new LoggerConfiguration()
```

Many sinks simply include all properties without further action required, so the thread id will be logged automatically.
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 or ThreadName to show up in the logging, you will need to create or modify your output template.
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` or `ThreadName` to show up in the logging, you will need to create or modify your output template.

```csharp
w.File(...., outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}")
```
Here, {Properties} can include not only ThreadId and ThreadName, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment and {ThreadName}, if you want to only add the thread name enrichment.
Here, \{Properties} can include not only `ThreadId` and `ThreadName`, but any other enrichment which is applied. Alternatively, \{ThreadId} could be used instead, if you want to only add the thread id enrichment and \{ThreadName}, if you want to only add the thread name enrichment.

An example, which also uses the Serilogs.Sinks.Async Nuget package, is below:

Expand All @@ -46,7 +46,7 @@ An example, which also uses the Serilogs.Sinks.Async Nuget package, is below:
```
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4><MyWorker>
```
Where, <4> is an example thread id and <MyWorker> is an example thread name.
Where, <4> is an example thread id and \<MyWorker> is an example thread name.

To use the enricher, first install the NuGet package:

Expand All @@ -55,7 +55,7 @@ Install-Package Serilog.Enrichers.Thread
```

Note:
The {ThreadName} property will only be attached when it is not null. Otherwise it will be omitted.
The \{ThreadName} property will only be attached when it is not null. Otherwise it will be omitted.
If you want to get this property always attached you can use the following:
```csharp
using Serilog.Enrichers;
Expand Down
14 changes: 13 additions & 1 deletion src/Serilog.Enrichers.Thread/Enrichers/ThreadIdEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@ public class ThreadIdEnricher : ILogEventEnricher
/// </summary>
public const string ThreadIdPropertyName = "ThreadId";

/// <summary>
/// The cached last created "ThreadId" property with some thread id. It is likely to be reused frequently so avoiding heap allocations.
/// </summary>
private LogEventProperty _lastValue;

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(new LogEventProperty(ThreadIdPropertyName, new ScalarValue(Environment.CurrentManagedThreadId)));
var threadId = Environment.CurrentManagedThreadId;

var last = _lastValue;
if (last == null || (int)((ScalarValue)last.Value).Value != threadId)
// no need to synchronize threads on write - just some of them will win
_lastValue = last = new LogEventProperty(ThreadIdPropertyName, new ScalarValue(threadId));

logEvent.AddPropertyIfAbsent(last);
}
}
}
12 changes: 11 additions & 1 deletion src/Serilog.Enrichers.Thread/Enrichers/ThreadNameEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ThreadNameEnricher : ILogEventEnricher
/// </summary>
public const string ThreadNamePropertyName = "ThreadName";

/// <summary>
/// The cached last created "ThreadName" property with some thread name. It is likely to be reused frequently so avoiding heap allocations.
/// </summary>
private LogEventProperty _lastValue;

/// <summary>
/// Enrich the log event.
/// </summary>
Expand All @@ -39,7 +44,12 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
var threadName = Thread.CurrentThread.Name;
if (threadName != null)
{
logEvent.AddPropertyIfAbsent(new LogEventProperty(ThreadNamePropertyName, new ScalarValue(threadName)));
var last = _lastValue;
if (last == null || (string)((ScalarValue)last.Value).Value != threadName)
// no need to synchronize threads on write - just some of them will win
_lastValue = last = new LogEventProperty(ThreadNamePropertyName, new ScalarValue(threadName));

logEvent.AddPropertyIfAbsent(last);
}
}
}
Expand Down