Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8d9bb6c
Support dependency injection in logging. Add AddOpenTelemetryEventSou…
CodeBlanch Jul 29, 2022
a3616bb
Merge branch 'main' into loggerprovider-dependencyinjection
CodeBlanch Jul 29, 2022
0836b09
CHANGELOG update.
CodeBlanch Jul 29, 2022
013ab59
Bug fixes and a test.
CodeBlanch Jul 29, 2022
9ee35f2
More fixes and more tests.
CodeBlanch Jul 29, 2022
c0fe823
Tweak comments for clarity.
CodeBlanch Jul 29, 2022
5b4ffc1
Added OpenTelemetryLoggerOptions.Services xml detail remarks.
CodeBlanch Jul 29, 2022
a1fe108
More tests.
CodeBlanch Jul 29, 2022
d2ba768
More tests.
CodeBlanch Jul 29, 2022
a8f8238
Test fix.
CodeBlanch Jul 29, 2022
6daf110
More tests.
CodeBlanch Jul 29, 2022
71cdfbf
Tests and fixes.
CodeBlanch Jul 29, 2022
0c73626
Warning cleanup.
CodeBlanch Jul 29, 2022
c5c93e0
Added resource test.
CodeBlanch Jul 29, 2022
3632366
Smooth out multiple resource configurations.
CodeBlanch Jul 29, 2022
e78734c
Resource chaining fix.
CodeBlanch Jul 29, 2022
8aeb08e
Remove throw for additional SetResourceBuilder calls.
CodeBlanch Jul 29, 2022
570ab37
Warning fixes.
CodeBlanch Jul 29, 2022
813bacc
Moved OpenTelemetryEventSourceLogEmitter extension to OpenTelemetryLo…
CodeBlanch Jul 30, 2022
6ffd623
Tweaks.
CodeBlanch Jul 30, 2022
fad5a6c
Switched from static to builder pattern.
CodeBlanch Aug 1, 2022
d477d8d
Tweaks.
CodeBlanch Aug 1, 2022
2c4195d
More tests.
CodeBlanch Aug 1, 2022
ce2a932
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 4, 2022
6284ff1
Fix CHANGELOG for release.
CodeBlanch Aug 4, 2022
d416e1b
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 5, 2022
2b26cb0
Tweaks.
CodeBlanch Aug 5, 2022
280ad5d
Test updates.
CodeBlanch Aug 5, 2022
251ff81
Added a detached configuration option.
CodeBlanch Aug 5, 2022
5f2b5b5
Prevent double registration to be consistent with tracer builder patt…
CodeBlanch Aug 5, 2022
28301d8
API review.
CodeBlanch Aug 5, 2022
e389a5d
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 5, 2022
7cc2865
Warning cleanup.
CodeBlanch Aug 5, 2022
8ecfdaf
Build fixes.
CodeBlanch Aug 5, 2022
7057343
Test fix.
CodeBlanch Aug 5, 2022
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
Prev Previous commit
Next Next commit
Tweaks.
  • Loading branch information
CodeBlanch committed Aug 1, 2022
commit d477d8d016fe03e98321c65959808e131d2c2a76
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static OpenTelemetryLoggerOptions AddEventSourceLogEmitter(
Guard.ThrowIfNull(options);
Guard.ThrowIfNull(shouldListenToFunc);

options.Services.TryAddSingleton<EventSourceManager>();
options.ConfigureServices(services => services.TryAddSingleton<EventSourceManager>());

options.ConfigureProvider((sp, provider) =>
{
Expand Down
6 changes: 2 additions & 4 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Act
* needed.
*/

var options = new OpenTelemetryLoggerOptions
{
Services = builder.Services,
};
var options = new OpenTelemetryLoggerOptions(builder.Services);

configure(options);

/*
Expand Down
42 changes: 40 additions & 2 deletions src/OpenTelemetry/Logs/Options/OpenTelemetryLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,29 @@ public class OpenTelemetryLoggerOptions
internal readonly List<BaseProcessor<LogRecord>> Processors = new();
internal ResourceBuilder? ResourceBuilder;
internal List<Action<IServiceProvider, OpenTelemetryLoggerProvider>>? ConfigurationActions = new();
internal IServiceCollection? Services;

private const bool DefaultIncludeScopes = false;
private const bool DefaultIncludeFormattedMessage = false;
private const bool DefaultParseStateValues = false;

private IServiceCollection? services;
private bool? includeScopes;
private bool? includeFormattedMessage;
private bool? parseStateValues;

/// <summary>
/// Initializes a new instance of the <see cref="OpenTelemetryLoggerOptions"/> class.
/// </summary>
public OpenTelemetryLoggerOptions()
: this(services: null)
{
}

internal OpenTelemetryLoggerOptions(IServiceCollection? services)
{
this.services = services;
}

/// <summary>
/// Gets or sets a value indicating whether or not log scopes should be
/// included on generated <see cref="LogRecord"/>s. Default value:
Expand Down Expand Up @@ -82,6 +95,8 @@ public bool ParseStateValues
set => this.parseStateValues = value;
}

internal IServiceCollection? Services => this.services;

/// <summary>
/// Adds processor to the options.
/// </summary>
Expand Down Expand Up @@ -173,7 +188,7 @@ public OpenTelemetryLoggerOptions ConfigureServices(
{
Guard.ThrowIfNull(configure);

var services = this.Services;
var services = this.services;

if (services == null)
{
Expand Down Expand Up @@ -208,6 +223,29 @@ public OpenTelemetryLoggerOptions ConfigureProvider(
return this;
}

internal OpenTelemetryLoggerProvider Build()
{
var services = this.services;

if (services == null)
{
throw new NotSupportedException("LoggerProviderBuilder build method cannot be called multiple times.");
}

this.services = null;

var serviceProvider = services.BuildServiceProvider();

var finalOptions = serviceProvider.GetRequiredService<IOptionsMonitor<OpenTelemetryLoggerOptions>>().CurrentValue;

this.ApplyTo(finalOptions);

return new OpenTelemetryLoggerProvider(
finalOptions,
serviceProvider,
ownsServiceProvider: true);
}

internal void ApplyTo(OpenTelemetryLoggerOptions other)
{
Debug.Assert(other != null, "other instance was null");
Expand Down
31 changes: 2 additions & 29 deletions src/OpenTelemetry/Logs/Options/OpenTelemetryLoggerOptionsSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,16 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace OpenTelemetry.Logs;

internal sealed class OpenTelemetryLoggerOptionsSdk : OpenTelemetryLoggerOptions
{
public OpenTelemetryLoggerOptionsSdk(Action<OpenTelemetryLoggerOptions>? configure)
: base(new ServiceCollection())
{
var services = new ServiceCollection();

services.AddOptions();

this.Services = services;
this.ConfigureServices(services => services.AddOptions());

configure?.Invoke(this);
}

public OpenTelemetryLoggerProvider Build()
{
var services = this.Services;

if (services == null)
{
throw new NotSupportedException("LoggerProviderBuilder build method cannot be called multiple times.");
}

this.Services = null;

var serviceProvider = services.BuildServiceProvider();

var finalOptions = serviceProvider.GetRequiredService<IOptionsMonitor<OpenTelemetryLoggerOptions>>().CurrentValue;

this.ApplyTo(finalOptions);

return new OpenTelemetryLoggerProvider(
finalOptions,
serviceProvider,
ownsServiceProvider: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void ServiceCollectionAddOpenTelemetryProcessorThroughDependencyWithRegis
{
if (!registerOutside)
{
options.Services!.AddSingleton<CustomProcessor>();
options.ConfigureServices(services => services.AddSingleton<CustomProcessor>());
}

options.AddProcessor<CustomProcessor>();
Expand Down