From a7c0e78195d50cb3012278a9b0034ddb8978f788 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 13 Mar 2020 11:27:42 +0100 Subject: [PATCH 1/2] Feature - provide extensions to add Serilog enrichers --- docs/features/telemetry-enrichment.md | 30 ++++++++++++ .../ApplicationEnricher.cs | 2 +- ...LoggerEnrichmentConfigurationExtensions.cs | 48 +++++++++++++++++++ .../Serilog/ApplicationEnricherTests.cs | 2 +- .../Serilog/KubernetesEnricherTests.cs | 2 +- .../Serilog/VersionEnricherTests.cs | 2 +- 6 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/Arcus.Observability.Telemetry.Serilog.Enrichers/Extensions/LoggerEnrichmentConfigurationExtensions.cs diff --git a/docs/features/telemetry-enrichment.md b/docs/features/telemetry-enrichment.md index 78110bb6..d61678d7 100644 --- a/docs/features/telemetry-enrichment.md +++ b/docs/features/telemetry-enrichment.md @@ -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() @@ -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() @@ -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")) diff --git a/src/Arcus.Observability.Telemetry.Serilog.Enrichers/ApplicationEnricher.cs b/src/Arcus.Observability.Telemetry.Serilog.Enrichers/ApplicationEnricher.cs index 2a721c3e..9a9e9868 100644 --- a/src/Arcus.Observability.Telemetry.Serilog.Enrichers/ApplicationEnricher.cs +++ b/src/Arcus.Observability.Telemetry.Serilog.Enrichers/ApplicationEnricher.cs @@ -19,7 +19,7 @@ public class ApplicationEnricher : ILogEventEnricher /// The name of the application component. public ApplicationEnricher(string componentName) { - Guard.NotNullOrWhitespace(componentName, nameof(componentName)); + Guard.NotNullOrWhitespace(componentName, nameof(componentName), "Application component name cannot be blank"); _componentValue = componentName; } diff --git a/src/Arcus.Observability.Telemetry.Serilog.Enrichers/Extensions/LoggerEnrichmentConfigurationExtensions.cs b/src/Arcus.Observability.Telemetry.Serilog.Enrichers/Extensions/LoggerEnrichmentConfigurationExtensions.cs new file mode 100644 index 00000000..6647ee0c --- /dev/null +++ b/src/Arcus.Observability.Telemetry.Serilog.Enrichers/Extensions/LoggerEnrichmentConfigurationExtensions.cs @@ -0,0 +1,48 @@ +using Arcus.Observability.Telemetry.Serilog.Enrichers; +using GuardNet; +using Serilog.Configuration; + +// ReSharper disable once CheckNamespace +namespace Serilog +{ + /// + /// Adds user-friendly extensions to append additional Serilog enrichers to the . + /// + public static class LoggerEnrichmentConfigurationExtensions + { + /// + /// Adds the to the logger enrichment configuration which adds the current runtime version (i.e. 'version' = '1.0.0'). + /// + /// The configuration to add the enricher. + public static LoggerConfiguration WithVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration) + { + Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration)); + + return enrichmentConfiguration.With(); + } + + /// + /// Adds the to the logger enrichment configuration which adds the given application's . + /// + /// The configuration to add the enricher. + /// The name of the application component. + 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)); + } + + /// + /// Adds the to the logger enrichment configuration which adds Kubernetes information from the environment. + /// + /// The configuration to add the enricher. + public static LoggerConfiguration WithKubernetesInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration) + { + Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration)); + + return enrichmentConfiguration.With(); + } + } +} diff --git a/src/Arcus.Observability.Tests.Unit/Serilog/ApplicationEnricherTests.cs b/src/Arcus.Observability.Tests.Unit/Serilog/ApplicationEnricherTests.cs index 25850ddd..6c3780e4 100644 --- a/src/Arcus.Observability.Tests.Unit/Serilog/ApplicationEnricherTests.cs +++ b/src/Arcus.Observability.Tests.Unit/Serilog/ApplicationEnricherTests.cs @@ -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(); diff --git a/src/Arcus.Observability.Tests.Unit/Serilog/KubernetesEnricherTests.cs b/src/Arcus.Observability.Tests.Unit/Serilog/KubernetesEnricherTests.cs index b5124c6d..417bb12f 100644 --- a/src/Arcus.Observability.Tests.Unit/Serilog/KubernetesEnricherTests.cs +++ b/src/Arcus.Observability.Tests.Unit/Serilog/KubernetesEnricherTests.cs @@ -54,7 +54,7 @@ public void LogEventWithNodeNameProperty_WithKubernetesEnricher_HasEnvironmentIn var spy = new InMemoryLogSink(); ILogger logger = new LoggerConfiguration() - .Enrich.With() + .Enrich.WithKubernetesInfo() .WriteTo.Sink(spy) .CreateLogger(); diff --git a/src/Arcus.Observability.Tests.Unit/Serilog/VersionEnricherTests.cs b/src/Arcus.Observability.Tests.Unit/Serilog/VersionEnricherTests.cs index 7799218c..8b5cb3ad 100644 --- a/src/Arcus.Observability.Tests.Unit/Serilog/VersionEnricherTests.cs +++ b/src/Arcus.Observability.Tests.Unit/Serilog/VersionEnricherTests.cs @@ -18,7 +18,7 @@ public void LogEvent_WithVersionEnricher_HasVersionProperty() var configuration = new LoggerConfiguration() .WriteTo.Sink(spy) - .Enrich.With(); + .Enrich.WithVersion(); ILogger logger = configuration.CreateLogger(); From 94b4fc073e108b5388e92b0db890eb2d117b5746 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 13 Mar 2020 12:59:53 +0100 Subject: [PATCH 2/2] pr-sug: remove directly enricher approach --- docs/features/telemetry-enrichment.md | 30 +-------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/docs/features/telemetry-enrichment.md b/docs/features/telemetry-enrichment.md index d61678d7..30bdd599 100644 --- a/docs/features/telemetry-enrichment.md +++ b/docs/features/telemetry-enrichment.md @@ -32,16 +32,6 @@ ILogger logger = new LoggerConfiguration() 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() - .CreateLogger(); - -logger.Information("This event will be enriched with the runtime assembly product version"); -``` - ## Kubernetes Enricher The `Arcus.Observability.Telemetry.Serilog` library provides a [Kubernetes](https://kubernetes.io/) [Serilog enricher](https://github.com/serilog/serilog/wiki/Enrichment) @@ -64,16 +54,6 @@ ILogger logger = new LoggerConfiguration() logger.Information("This event will be enriched with the Kubernetes environment information"); ``` -Or using the enricher directly: - -```csharp -ILogger logger = new LoggerConfiguration() - .Enrich.With() - .CreateLogger(); - -logger.Information("This event will be enriched with the Kubernetes environment information"); -``` - ## Application Enricher The `Arcus.Observability.Telemetry.Serilog` library provides a [Serilog enricher](https://github.com/serilog/serilog/wiki/Enrichment) @@ -93,12 +73,4 @@ ILogger logger = new LoggerConfiguration() 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")) - .CreateLogger(); - -logger.Information("This event will be enriched with the application component's name"); -``` \ No newline at end of file +[← back](/)