Skip to content

Commit 1fe8dfa

Browse files
authored
NLog Layout (#44)
Add an NLog Layout for ECS
1 parent 7738c62 commit 1fe8dfa

File tree

21 files changed

+833
-25
lines changed

21 files changed

+833
-25
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ Official NuGet packages can be referenced from [NuGet.org](https://www.nuget.org
1414

1515
| Package Name | Purpose | Download |
1616
| ----------------------- | ---------------- | -----------------|
17-
| `Elastic.CommonSchema` | Foundational project that contains a full C# representation of ECS, used by the other integrations listed. | [![NuGet Release][ElasticCommonSchema-image]][ElasticCommonSchema-nuget-url] |
18-
| `Elastic.CommonSchema.Serilog` | Formats a Serilog log message into a JSON representation that can be indexed into Elasticsearch. | [![NuGet Release][ElasticCommonSchemaSerilog-image]][ElasticCommonSchemaSerilog-nuget-url] |
19-
| `Elastic.Apm.SerilogEnricher` | Adds transaction id and trace id to every Serilog log message that is created during a transaction. This works in conjunction with the Elastic.CommonSchema.Serilog package and forms a solution to distributed tracing with Serilog. | [![NuGet Release][ElasticApmSerilogEnricher-image]][ElasticApmSerilogEnricher-nuget-url] |
20-
| `Elastic.Apm.NLog` | Introduces two special placeholder variables (ElasticApmTraceId and ElasticApmTransactionId) for use within your NLog templates. | [![NuGet Release][ElasticApmNLog-image]][ElasticApmNLog-nuget-url] |
21-
| `Elastic.CommonSchema.BenchmarkDotNetExporter` | An exporter for BenchmarkDotnet that can index benchmarking results directly into Elasticsearch, which can be helpful for detecting code-related performance problems over time. | [![NuGet Release][ElasticBenchmarkDotNetExporter-image]][ElasticBenchmarkDotNetExporter-nuget-url] |
17+
| `Elastic.CommonSchema` | Foundational project that contains a full C# representation of ECS, used by the other integrations listed. | [![NuGet Release][ElasticCommonSchema-image]][ElasticCommonSchema-nuget-url] |
18+
| `Elastic.CommonSchema.Serilog` | Formats a Serilog log message into a JSON representation that can be indexed into Elasticsearch. | [![NuGet Release][ElasticCommonSchemaSerilog-image]][ElasticCommonSchemaSerilog-nuget-url] |
19+
| `Elastic.CommonSchema.NLog` | Formats an NLog message into a JSON representation that can be indexed into Elasticsearch. | [![NuGet Release][ElasticCommonSchemaNLog-image]][ElasticCommonSchemaNLog-nuget-url] |
20+
| `Elastic.Apm.SerilogEnricher` | Adds transaction id and trace id to every Serilog log message that is created during a transaction. This works in conjunction with the Elastic.CommonSchema.Serilog package and forms a solution to distributed tracing with Serilog. | [![NuGet Release][ElasticApmSerilogEnricher-image]][ElasticApmSerilogEnricher-nuget-url] |
21+
| `Elastic.Apm.NLog` | Introduces two special placeholder variables (ElasticApmTraceId and ElasticApmTransactionId) for use within your NLog templates. | [![NuGet Release][ElasticApmNLog-image]][ElasticApmNLog-nuget-url] |
22+
| `Elastic.CommonSchema.BenchmarkDotNetExporter` | An exporter for BenchmarkDotnet that can index benchmarking results directly into Elasticsearch, which can be helpful for detecting code-related performance problems over time. | [![NuGet Release][ElasticBenchmarkDotNetExporter-image]][ElasticBenchmarkDotNetExporter-nuget-url] |
2223

2324
[ElasticCommonSchema-nuget-url]:https://www.nuget.org/packages/Elastic.CommonSchema/
2425
[ElasticCommonSchema-image]:https://img.shields.io/nuget/v/Elastic.CommonSchema.svg
2526

2627
[ElasticCommonSchemaSerilog-nuget-url]:https://www.nuget.org/packages/Elastic.CommonSchema.Serilog/
2728
[ElasticCommonSchemaSerilog-image]:https://img.shields.io/nuget/v/Elastic.CommonSchema.Serilog.svg
2829

30+
[ElasticCommonSchemaNLog-nuget-url]:https://www.nuget.org/packages/Elastic.CommonSchema.NLog/
31+
[ElasticCommonSchemaNLog-image]:https://img.shields.io/nuget/v/Elastic.CommonSchema.NLog.svg
32+
2933
[ElasticApmSerilogEnricher-nuget-url]:https://www.nuget.org/packages/Elastic.Apm.SerilogEnricher/
3034
[ElasticApmSerilogEnricher-image]:https://img.shields.io/nuget/v/Elastic.Apm.SerilogEnricher.svg
3135

@@ -53,6 +57,19 @@ var logger = new LoggerConfiguration()
5357
.CreateLogger();
5458
```
5559

60+
### [Elastic.CommonSchema.NLog](https://github.com/elastic/ecs-dotnet/tree/master/src/Elastic.CommonSchema.NLog)
61+
62+
Formats an NLog event into a JSON representation that adheres to the Elastic Common Schema. [Learn more...](https://github.com/elastic/ecs-dotnet/tree/master/src/Elastic.CommonSchema.NLog)
63+
64+
```csharp
65+
Layout.Register<EcsLayout>("EcsLayout"); // Register the ECS layout.
66+
var config = new Config.LoggingConfiguration();
67+
var memoryTarget = new EventInfoMemoryTarget { Layout = Layout.FromString("EcsLayout") }; // Use the layout.
68+
config.AddRule(LogLevel.Debug, LogLevel.Fatal, memoryTarget);
69+
var factory = new LogFactory(config);
70+
var logger = factory.GetCurrentClassLogger();
71+
```
72+
5673
## APM
5774

5875
### [Elastic.Apm.SerilogEnricher](https://github.com/elastic/ecs-dotnet/tree/master/src/Elastic.Apm.SerilogEnricher)

src/Elastic.Apm.NLog/ApmTraceIdLayoutRenderer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
namespace Elastic.Apm.NLog
1111
{
12-
[LayoutRenderer("ElasticApmTraceId")]
12+
[LayoutRenderer(Name)]
1313
[ThreadSafe]
1414
public class ApmTraceIdLayoutRenderer : LayoutRenderer
1515
{
16+
public const string Name = "ElasticApmTraceId";
17+
1618
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
1719
{
18-
if (!Agent.IsConfigured) return;
19-
if (Agent.Tracer.CurrentTransaction == null) return;
20+
if (!Agent.IsConfigured || Agent.Tracer?.CurrentTransaction == null)
21+
return;
2022

2123
builder.Append(Agent.Tracer.CurrentTransaction.TraceId);
2224
}

src/Elastic.Apm.NLog/ApmTransactionIdLayoutRenderer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
namespace Elastic.Apm.NLog
1111
{
12-
[LayoutRenderer("ElasticApmTransactionId")]
12+
[LayoutRenderer(Name)]
1313
[ThreadSafe]
1414
public class ApmTransactionIdLayoutRenderer : LayoutRenderer
1515
{
16+
public const string Name = "ElasticApmTransactionId";
17+
1618
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
1719
{
18-
if (!Agent.IsConfigured) return;
19-
if (Agent.Tracer.CurrentTransaction == null) return;
20+
if (!Agent.IsConfigured || Agent.Tracer?.CurrentTransaction == null)
21+
return;
2022

2123
builder.Append(Agent.Tracer.CurrentTransaction.Id);
2224
}

src/Elastic.Apm.SerilogEnricher/ElasticApmEnricher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public sealed class ElasticApmEnricher : ILogEventEnricher
1515
{
1616
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
1717
{
18-
if (!Agent.IsConfigured) return;
19-
if (Agent.Tracer.CurrentTransaction == null) return;
18+
if (!Agent.IsConfigured || Agent.Tracer.CurrentTransaction == null)
19+
return;
2020

2121
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
2222
"ElasticApmTransactionId", Agent.Tracer.CurrentTransaction.Id));

src/Elastic.Apm.SerilogEnricher/ElasticApmEnricherExtension.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ public static class ElasticApmEnricherExtension
1717
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
1818
/// <returns>Configuration object allowing method chaining.</returns>
1919
/// <exception cref="ArgumentNullException">If <paramref name="enrichmentConfiguration" /> is null.</exception>
20-
public static LoggerConfiguration WithElasticApmCorrelationInfo(
21-
this LoggerEnrichmentConfiguration enrichmentConfiguration
22-
)
20+
public static LoggerConfiguration WithElasticApmCorrelationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration)
2321
{
24-
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
22+
if (enrichmentConfiguration == null)
23+
throw new ArgumentNullException(nameof(enrichmentConfiguration));
2524

2625
return enrichmentConfiguration.With<ElasticApmEnricher>();
2726
}

0 commit comments

Comments
 (0)