Skip to content

Commit

Permalink
NLog Layout (#44)
Browse files Browse the repository at this point in the history
Add an NLog Layout for ECS
  • Loading branch information
codebrain authored Feb 26, 2020
1 parent 7738c62 commit 1fe8dfa
Show file tree
Hide file tree
Showing 21 changed files with 833 additions and 25 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ Official NuGet packages can be referenced from [NuGet.org](https://www.nuget.org

| Package Name | Purpose | Download |
| ----------------------- | ---------------- | -----------------|
| `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] |
| `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] |
| `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] |
| `Elastic.Apm.NLog` | Introduces two special placeholder variables (ElasticApmTraceId and ElasticApmTransactionId) for use within your NLog templates. | [![NuGet Release][ElasticApmNLog-image]][ElasticApmNLog-nuget-url] |
| `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] |
| `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] |
| `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] |
| `Elastic.CommonSchema.NLog` | Formats an NLog message into a JSON representation that can be indexed into Elasticsearch. | [![NuGet Release][ElasticCommonSchemaNLog-image]][ElasticCommonSchemaNLog-nuget-url] |
| `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] |
| `Elastic.Apm.NLog` | Introduces two special placeholder variables (ElasticApmTraceId and ElasticApmTransactionId) for use within your NLog templates. | [![NuGet Release][ElasticApmNLog-image]][ElasticApmNLog-nuget-url] |
| `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] |

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

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

[ElasticCommonSchemaNLog-nuget-url]:https://www.nuget.org/packages/Elastic.CommonSchema.NLog/
[ElasticCommonSchemaNLog-image]:https://img.shields.io/nuget/v/Elastic.CommonSchema.NLog.svg

[ElasticApmSerilogEnricher-nuget-url]:https://www.nuget.org/packages/Elastic.Apm.SerilogEnricher/
[ElasticApmSerilogEnricher-image]:https://img.shields.io/nuget/v/Elastic.Apm.SerilogEnricher.svg

Expand Down Expand Up @@ -53,6 +57,19 @@ var logger = new LoggerConfiguration()
.CreateLogger();
```

### [Elastic.CommonSchema.NLog](https://github.com/elastic/ecs-dotnet/tree/master/src/Elastic.CommonSchema.NLog)

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)

```csharp
Layout.Register<EcsLayout>("EcsLayout"); // Register the ECS layout.
var config = new Config.LoggingConfiguration();
var memoryTarget = new EventInfoMemoryTarget { Layout = Layout.FromString("EcsLayout") }; // Use the layout.
config.AddRule(LogLevel.Debug, LogLevel.Fatal, memoryTarget);
var factory = new LogFactory(config);
var logger = factory.GetCurrentClassLogger();
```

## APM

### [Elastic.Apm.SerilogEnricher](https://github.com/elastic/ecs-dotnet/tree/master/src/Elastic.Apm.SerilogEnricher)
Expand Down
8 changes: 5 additions & 3 deletions src/Elastic.Apm.NLog/ApmTraceIdLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

namespace Elastic.Apm.NLog
{
[LayoutRenderer("ElasticApmTraceId")]
[LayoutRenderer(Name)]
[ThreadSafe]
public class ApmTraceIdLayoutRenderer : LayoutRenderer
{
public const string Name = "ElasticApmTraceId";

protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (!Agent.IsConfigured) return;
if (Agent.Tracer.CurrentTransaction == null) return;
if (!Agent.IsConfigured || Agent.Tracer?.CurrentTransaction == null)
return;

builder.Append(Agent.Tracer.CurrentTransaction.TraceId);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Elastic.Apm.NLog/ApmTransactionIdLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

namespace Elastic.Apm.NLog
{
[LayoutRenderer("ElasticApmTransactionId")]
[LayoutRenderer(Name)]
[ThreadSafe]
public class ApmTransactionIdLayoutRenderer : LayoutRenderer
{
public const string Name = "ElasticApmTransactionId";

protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (!Agent.IsConfigured) return;
if (Agent.Tracer.CurrentTransaction == null) return;
if (!Agent.IsConfigured || Agent.Tracer?.CurrentTransaction == null)
return;

builder.Append(Agent.Tracer.CurrentTransaction.Id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Apm.SerilogEnricher/ElasticApmEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public sealed class ElasticApmEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (!Agent.IsConfigured) return;
if (Agent.Tracer.CurrentTransaction == null) return;
if (!Agent.IsConfigured || Agent.Tracer.CurrentTransaction == null)
return;

logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
"ElasticApmTransactionId", Agent.Tracer.CurrentTransaction.Id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public static class ElasticApmEnricherExtension
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="enrichmentConfiguration" /> is null.</exception>
public static LoggerConfiguration WithElasticApmCorrelationInfo(
this LoggerEnrichmentConfiguration enrichmentConfiguration
)
public static LoggerConfiguration WithElasticApmCorrelationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
if (enrichmentConfiguration == null)
throw new ArgumentNullException(nameof(enrichmentConfiguration));

return enrichmentConfiguration.With<ElasticApmEnricher>();
}
Expand Down
Loading

0 comments on commit 1fe8dfa

Please sign in to comment.