Skip to content
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

Feature - provide extensions to add Serilog enrichers #41

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions docs/features/telemetry-enrichment.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ Value: `1.0.0-preview`

**Usage**

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.WithVersion()
.CreateLogger();

logger.Information("This event will be enriched with the runtime assembly product version");
```

Or using the enricher directly:

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.With<VersionEnricher>()
tomkerkhove marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -46,6 +56,16 @@ that adds several machine information from the environment (variables).

**Usage**

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.WithKubernetesInfo()
.CreateLogger();

logger.Information("This event will be enriched with the Kubernetes environment information");
```

Or using the enricher directly:

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.With<KubernetesEnricher>()
Expand All @@ -65,6 +85,16 @@ Value: `My application component`

**Usage**

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.WithComponentName("My application component")
.CreateLogger();

logger.Information("This event will be enriched with the application component's name");
```

Or using the enricher directly:

```csharp
ILogger logger = new LoggerConfiguration()
.Enrich.With(new ApplicationEnricher("My application component"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ApplicationEnricher : ILogEventEnricher
/// <param name="componentName">The name of the application component.</param>
public ApplicationEnricher(string componentName)
{
Guard.NotNullOrWhitespace(componentName, nameof(componentName));
Guard.NotNullOrWhitespace(componentName, nameof(componentName), "Application component name cannot be blank");

_componentValue = componentName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Arcus.Observability.Telemetry.Serilog.Enrichers;
using GuardNet;
using Serilog.Configuration;

// ReSharper disable once CheckNamespace
namespace Serilog
{
/// <summary>
/// Adds user-friendly extensions to append additional Serilog enrichers to the <see cref="LoggerConfiguration"/>.
/// </summary>
public static class LoggerEnrichmentConfigurationExtensions
{
/// <summary>
/// Adds the <see cref="VersionEnricher"/> to the logger enrichment configuration which adds the current runtime version (i.e. 'version' = '1.0.0').
/// </summary>
/// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
public static LoggerConfiguration WithVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration));

return enrichmentConfiguration.With<VersionEnricher>();
}

/// <summary>
/// Adds the <see cref="ApplicationEnricher"/> to the logger enrichment configuration which adds the given application's <paramref name="componentName"/>.
/// </summary>
/// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
/// <param name="componentName">The name of the application component.</param>
public static LoggerConfiguration WithComponentName(this LoggerEnrichmentConfiguration enrichmentConfiguration, string componentName)
{
Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
Guard.NotNullOrWhitespace(componentName, nameof(componentName), "Application component name cannot be blank");

return enrichmentConfiguration.With(new ApplicationEnricher(componentName));
}

/// <summary>
/// Adds the <see cref="KubernetesEnricher"/> to the logger enrichment configuration which adds Kubernetes information from the environment.
/// </summary>
/// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
public static LoggerConfiguration WithKubernetesInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration));

return enrichmentConfiguration.With<KubernetesEnricher>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void LogEvent_WithApplicationEnricher_HasComponentName()
string componentName = $"component-{Guid.NewGuid()}";
var spy = new InMemoryLogSink();
ILogger logger = new LoggerConfiguration()
.Enrich.With(new ApplicationEnricher(componentName))
.Enrich.WithComponentName(componentName)
.WriteTo.Sink(spy)
.CreateLogger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void LogEventWithNodeNameProperty_WithKubernetesEnricher_HasEnvironmentIn

var spy = new InMemoryLogSink();
ILogger logger = new LoggerConfiguration()
.Enrich.With<KubernetesEnricher>()
.Enrich.WithKubernetesInfo()
.WriteTo.Sink(spy)
.CreateLogger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void LogEvent_WithVersionEnricher_HasVersionProperty()
var configuration =
new LoggerConfiguration()
.WriteTo.Sink(spy)
.Enrich.With<VersionEnricher>();
.Enrich.WithVersion();

ILogger logger = configuration.CreateLogger();

Expand Down