Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public void Setup()
{
Dsn = DsnSamples.ValidDsn,
EnableLogs = true,
ExperimentalLogging =
{
MinimumLogLevel = LogLevel.Information,
}
};
options.SetBeforeSendLog((SentryLog log) =>
{
Expand Down
1 change: 0 additions & 1 deletion samples/Sentry.Samples.ME.Logging/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
// Optionally configure options: The default values are:
options.MinimumBreadcrumbLevel = LogLevel.Information; // It requires at least this level to store breadcrumb
options.MinimumEventLevel = LogLevel.Error; // This level or above will result in event sent to Sentry
options.ExperimentalLogging.MinimumLogLevel = LogLevel.Trace; // This level or above will result in log sent to Sentry

// This option enables Logs sent to Sentry.
options.EnableLogs = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ internal class BindableSentryLoggingOptions : BindableSentryOptions
public LogLevel? MinimumEventLevel { get; set; }
public bool? InitializeSdk { get; set; }

public BindableSentryLoggingExperimentalOptions ExperimentalLogging { get; set; } = new();

internal sealed class BindableSentryLoggingExperimentalOptions
{
public LogLevel? MinimumLogLevel { get; set; }
}

public void ApplyTo(SentryLoggingOptions options)
{
base.ApplyTo(options);
options.MinimumBreadcrumbLevel = MinimumBreadcrumbLevel ?? options.MinimumBreadcrumbLevel;
options.MinimumEventLevel = MinimumEventLevel ?? options.MinimumEventLevel;
options.InitializeSdk = InitializeSdk ?? options.InitializeSdk;

options.ExperimentalLogging.MinimumLogLevel = ExperimentalLogging.MinimumLogLevel ?? options.ExperimentalLogging.MinimumLogLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal static ILoggingBuilder AddSentry<TOptions>(
builder.AddFilter<SentryLoggerProvider>(_ => true);

// Logs from the SentryLogger should not flow to the SentryStructuredLogger as this may cause recursive invocations.
// Filtering of logs is handled in SentryStructuredLogger, using SentryOptions.MinimumLogLevel
// Filtering of structured logs is handled by Microsoft.Extensions.Configuration and Microsoft.Extensions.Options.
builder.AddFilter<SentryStructuredLoggerProvider>(static (string? categoryName, LogLevel logLevel) =>
{
return categoryName is null
Expand Down
35 changes: 0 additions & 35 deletions src/Sentry.Extensions.Logging/SentryLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,4 @@ public class SentryLoggingOptions : SentryOptions
/// List of callbacks to be invoked when initializing the SDK
/// </summary>
internal Action<Scope>[] ConfigureScopeCallbacks { get; set; } = Array.Empty<Action<Scope>>();

/// <summary>
/// Experimental Sentry Logging features.
/// </summary>
/// <remarks>
/// This and related experimental APIs may change in the future.
/// </remarks>
[Experimental(Infrastructure.DiagnosticId.ExperimentalFeature)]
public SentryLoggingExperimentalOptions ExperimentalLogging { get; set; } = new();

/// <summary>
/// Experimental Sentry Logging options.
/// </summary>
/// <remarks>
/// This and related experimental APIs may change in the future.
/// </remarks>
[Experimental(Infrastructure.DiagnosticId.ExperimentalFeature)]
public sealed class SentryLoggingExperimentalOptions
{
internal SentryLoggingExperimentalOptions()
{
}

/// <summary>
/// Gets or sets the minimum log level.
/// <para>This API is experimental and it may change in the future.</para>
/// </summary>
/// <remarks>
/// Logs with this level or higher will be stored as <see cref="SentryLog"/>.
/// </remarks>
/// <value>
/// The minimum log level.
/// </value>
public LogLevel MinimumLogLevel { get; set; } = LogLevel.Trace;
}
}
3 changes: 1 addition & 2 deletions src/Sentry.Extensions.Logging/SentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public bool IsEnabled(LogLevel logLevel)
{
return _hub.IsEnabled
&& _options.EnableLogs
&& logLevel != LogLevel.None
&& logLevel >= _options.ExperimentalLogging.MinimumLogLevel;
&& logLevel != LogLevel.None;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,10 @@ namespace Sentry.Extensions.Logging
public class SentryLoggingOptions : Sentry.SentryOptions
{
public SentryLoggingOptions() { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public Sentry.Extensions.Logging.SentryLoggingOptions.SentryLoggingExperimentalOptions ExperimentalLogging { get; set; }
public bool InitializeSdk { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public sealed class SentryLoggingExperimentalOptions
{
public Microsoft.Extensions.Logging.LogLevel MinimumLogLevel { get; set; }
}
}
public static class SentryLoggingOptionsExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,10 @@ namespace Sentry.Extensions.Logging
public class SentryLoggingOptions : Sentry.SentryOptions
{
public SentryLoggingOptions() { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public Sentry.Extensions.Logging.SentryLoggingOptions.SentryLoggingExperimentalOptions ExperimentalLogging { get; set; }
public bool InitializeSdk { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public sealed class SentryLoggingExperimentalOptions
{
public Microsoft.Extensions.Logging.LogLevel MinimumLogLevel { get; set; }
}
}
public static class SentryLoggingOptionsExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,10 @@ namespace Sentry.Extensions.Logging
public class SentryLoggingOptions : Sentry.SentryOptions
{
public SentryLoggingOptions() { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public Sentry.Extensions.Logging.SentryLoggingOptions.SentryLoggingExperimentalOptions ExperimentalLogging { get; set; }
public bool InitializeSdk { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
[System.Diagnostics.CodeAnalysis.Experimental("SENTRY0001")]
public sealed class SentryLoggingExperimentalOptions
{
public Microsoft.Extensions.Logging.LogLevel MinimumLogLevel { get; set; }
}
}
public static class SentryLoggingOptionsExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ namespace Sentry.Extensions.Logging
public class SentryLoggingOptions : Sentry.SentryOptions
{
public SentryLoggingOptions() { }
public Sentry.Extensions.Logging.SentryLoggingOptions.SentryLoggingExperimentalOptions ExperimentalLogging { get; set; }
public bool InitializeSdk { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; }
public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
public sealed class SentryLoggingExperimentalOptions
{
public Microsoft.Extensions.Logging.LogLevel MinimumLogLevel { get; set; }
}
}
public static class SentryLoggingOptionsExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void Configure_BindsConfigurationToOptions()
MinimumEventLevel = LogLevel.Error,
InitializeSdk = true
};
expected.ExperimentalLogging.MinimumLogLevel = LogLevel.None;
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
Expand Down Expand Up @@ -109,8 +108,6 @@ public void Configure_BindsConfigurationToOptions()
["MinimumBreadcrumbLevel"] = expected.MinimumBreadcrumbLevel.ToString(),
["MinimumEventLevel"] = expected.MinimumEventLevel.ToString(),
["InitializeSdk"] = expected.InitializeSdk.ToString(),

["ExperimentalLogging:MinimumLogLevel"] = expected.ExperimentalLogging.MinimumLogLevel.ToString(),
})
.Build();

Expand Down Expand Up @@ -169,8 +166,6 @@ public void Configure_BindsConfigurationToOptions()
actual.MinimumBreadcrumbLevel.Should().Be(expected.MinimumBreadcrumbLevel);
actual.MinimumEventLevel.Should().Be(expected.MinimumEventLevel);
actual.InitializeSdk.Should().Be(expected.InitializeSdk);

actual.ExperimentalLogging.MinimumLogLevel.Should().Be(expected.ExperimentalLogging.MinimumLogLevel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ public Fixture()

EnableHub(true);
EnableLogs(true);
SetMinimumLogLevel(default);
}

public void EnableHub(bool isEnabled) => Hub.IsEnabled.Returns(isEnabled);
public void EnableLogs(bool isEnabled) => Options.Value.EnableLogs = isEnabled;
public void SetMinimumLogLevel(LogLevel logLevel) => Options.Value.ExperimentalLogging.MinimumLogLevel = logLevel;

public void WithActiveSpan(SentryId traceId, SpanId parentSpanId)
{
Expand Down Expand Up @@ -251,20 +249,18 @@ public void Log_WithoutEventName_CaptureLog()
}

[Theory]
[InlineData(true, true, LogLevel.Warning, LogLevel.Warning, true)]
[InlineData(false, true, LogLevel.Warning, LogLevel.Warning, false)]
[InlineData(true, false, LogLevel.Warning, LogLevel.Warning, false)]
[InlineData(true, true, LogLevel.Information, LogLevel.Warning, true)]
[InlineData(true, true, LogLevel.Error, LogLevel.Warning, false)]
public void IsEnabled_HubOptionsMinimumLogLevel_Returns(bool isHubEnabled, bool isLogsEnabled, LogLevel minimumLogLevel, LogLevel actualLogLevel, bool expectedIsEnabled)
[InlineData(true, true, LogLevel.Information, true)]
[InlineData(false, true, LogLevel.Information, false)]
[InlineData(true, false, LogLevel.Information, false)]
[InlineData(true, true, LogLevel.None, false)]
public void IsEnabled_HubAndOptions_Returns(bool isHubEnabled, bool isLogsEnabled, LogLevel logLevel, bool expectedIsEnabled)
{
_fixture.EnableHub(isHubEnabled);
_fixture.EnableLogs(isLogsEnabled);
_fixture.SetMinimumLogLevel(minimumLogLevel);
var logger = _fixture.GetSut();

var isEnabled = logger.IsEnabled(actualLogLevel);
logger.Log(actualLogLevel, "message");
var isEnabled = logger.IsEnabled(logLevel);
logger.Log(logLevel, "message");

isEnabled.Should().Be(expectedIsEnabled);
if (expectedIsEnabled)
Expand Down
19 changes: 0 additions & 19 deletions test/Sentry.Testing/BindableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,11 @@ private static KeyValuePair<PropertyInfo, object> GetDummyBindableValue(Property
{$"key1", $"{propertyInfo.Name}value1"},
{$"key2", $"{propertyInfo.Name}value2"}
},
not null when propertyType.FullName == "Sentry.Extensions.Logging.SentryLoggingOptions+SentryLoggingExperimentalOptions" => CreateSentryLoggingExperimentalOptions(),
_ => throw new NotSupportedException($"Unsupported property type on property {propertyInfo.Name}")
};
return new KeyValuePair<PropertyInfo, object>(propertyInfo, value);
}

private static object CreateSentryLoggingExperimentalOptions()
{
var options = Activator.CreateInstance("Sentry.Extensions.Logging", "Sentry.Extensions.Logging.SentryLoggingOptions+SentryLoggingExperimentalOptions", false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null, null);
var instance = options.Unwrap();
var property = instance.GetType().GetProperty("MinimumLogLevel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(instance, int.MaxValue);
return instance;
}

private static IEnumerable<KeyValuePair<string, string>> ToConfigValues(KeyValuePair<PropertyInfo, object> item)
{
var (prop, value) = item;
Expand All @@ -91,11 +81,6 @@ private static IEnumerable<KeyValuePair<string, string>> ToConfigValues(KeyValue
yield return new KeyValuePair<string, string>($"{prop.Name}:{kvp.Key}", kvp.Value);
}
}
else if (propertyType.FullName == "Sentry.Extensions.Logging.SentryLoggingOptions+SentryLoggingExperimentalOptions")
{
var property = value.GetType().GetProperty("MinimumLogLevel");
yield return new KeyValuePair<string, string>($"{prop.Name}:MinimumLogLevel", Convert.ToString(property.GetValue(value), CultureInfo.InvariantCulture));
}
else
{
yield return new KeyValuePair<string, string>(prop.Name, Convert.ToString(value, CultureInfo.InvariantCulture));
Expand Down Expand Up @@ -130,10 +115,6 @@ protected void AssertContainsExpectedPropertyValues(TOptions actual)
{
actualValue.Should().BeEquivalentTo(expectedValue);
}
else if (prop.PropertyType.FullName == "Sentry.Extensions.Logging.SentryLoggingOptions+SentryLoggingExperimentalOptions")
{
actualValue.Should().BeEquivalentTo(expectedValue);
}
else
{
actualValue.Should().Be(expectedValue);
Expand Down
Loading