Skip to content

Commit fdf2866

Browse files
committed
fixes
1 parent b4be17e commit fdf2866

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

docs/core/enrichment/custom-enricher.md

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,45 @@ the <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Ad
4949
```csharp
5050
public class CustomEnricher : ILogEnricher
5151
{
52-
// Your custom code
53-
5452
public void Enrich(IEnrichmentTagCollector collector)
5553
{
56-
// Call Add to add all required key/value pair to enrich logs with.
57-
foreach(var property in propertiesToEnrichWith)
58-
{
59-
collector.Add(propertyName, propertyValue);
60-
}
54+
collector.Add("customKey", "customValue");
6155
}
6256
}
6357

64-
...
65-
66-
var hostBuilder = new HostBuilder()
67-
.ConfigureServices((_, serviceCollection) =>
68-
{
69-
_ = serviceCollection.AddLogEnricher<CustomEnricher>());
70-
});
7158
```
7259

73-
It's also possible to configure manual instantiation of custom enrichers:
60+
And you register it as shown in the following code using <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:
7461

7562
```csharp
76-
public class AnotherEnricher : ILogEnricher() { }
77-
...
78-
79-
var hostBuilder = new HostBuilder()
80-
.ConfigureServices((_, serviceCollection) =>
81-
{
82-
_ = serviceCollection.AddLogEnricher(new AnotherEnricher()));
83-
});
63+
var builder = Host.CreateApplicationBuilder(args);
64+
builder.Logging.EnableEnrichment();
65+
builder.Services.AddLogEnricher<CustomEnricher>();
8466
```
8567

86-
Alternatively:
68+
It's also possible to configure manual instantiation of custom enrichers:
8769

8870
```csharp
89-
var hostApplicationBuilder = WebApplication.CreateBuilder();
90-
hostApplicationBuilder.Services.AddLogEnricher<CustomEnricher>();
71+
public class AnotherEnricher : ILogEnricher
72+
{
73+
private readonly string _key;
74+
private readonly object _value;
75+
public CustomEnricher(string key, object value)
76+
{
77+
_key = key;
78+
_value = value;
79+
}
80+
public void Enrich(IEnrichmentTagCollector collector)
81+
{
82+
collector.Add(_key, _value);
83+
}
84+
}
9185
```
9286

93-
and
87+
And you register it as shown in the following code <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher)>:
9488

9589
```csharp
96-
var hostApplicationBuilder = WebApplication.CreateBuilder();
97-
hostApplicationBuilder.Services.AddLogEnricher(new AnotherEnricher()));
90+
var builder = Host.CreateApplicationBuilder();
91+
builder.Logging.EnableEnrichment();
92+
builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue"));
9893
```

0 commit comments

Comments
 (0)