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 @@ -16,7 +16,7 @@ public class SentryStructuredLoggerBenchmarks
[GlobalSetup]
public void Setup()
{
SentryLoggingOptions options = new()
SentryOptions options = new()
{
Dsn = DsnSamples.ValidDsn,
};
Expand All @@ -34,7 +34,7 @@ public void Setup()
};

_hub = new Hub(options, DisabledHub.Instance);
_logger = new SentryStructuredLogger("CategoryName", options, _hub, clock, sdk);
_logger = new SentryStructuredLogger("CategoryName", _hub, clock, sdk);
_logRecord = new LogRecord(LogLevel.Information, new EventId(2025, "EventName"), new InvalidOperationException("exception-message"), "Number={Number}, Text={Text}", 2018, "message");
}

Expand Down
17 changes: 11 additions & 6 deletions samples/Sentry.Samples.GenericHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

builder.Logging.AddConfiguration(builder.Configuration);

// Initialise the Sentry SDK. The logging integration added below only forwards log messages to Sentry.
using var sentry = SentrySdk.Init(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set it here in code, via the SENTRY_DSN environment variable or in your
// appsettings.json file.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
builder.Logging.AddSentry(SamplesShared.Dsn);
#else
builder.Logging.AddSentry();
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
// Send user name and machine name
options.SendDefaultPii = true;
});

builder.Logging.AddSentry();

builder.Services.AddHostedService<SampleHostedService>();

Expand Down
4 changes: 1 addition & 3 deletions samples/Sentry.Samples.GenericHost/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
}
},
"Sentry": {
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"SendDefaultPii": true // Send user name and machine name
"MinimumEventLevel": "Warning"
}
}
48 changes: 25 additions & 23 deletions samples/Sentry.Samples.ME.Logging/Program.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
using Microsoft.Extensions.Logging;
using Sentry.Extensions.Logging;

using var loggerFactory = LoggerFactory.Create(builder =>
// Initialise the Sentry SDK. The logging integration added below only forwards log messages to Sentry.
using var sentry = SentrySdk.Init(options =>
{
builder.AddConsole();
builder.AddSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif

// Set to true to SDK debugging to see the internal messages through the logging library.
options.Debug = false;
// Configure the level of Sentry internal logging
options.DiagnosticLevel = SentryLevel.Debug;
// Set to true to SDK debugging to see the internal messages through the logging library.
options.Debug = false;
// Configure the level of Sentry internal logging
options.DiagnosticLevel = SentryLevel.Debug;

options.MaxBreadcrumbs = 150; // Increasing from default 100
options.Release = "e386dfd"; // If not set here, SDK looks for it on main assembly's AssemblyInformationalVersion and AssemblyVersion

options.SetBeforeSendLog(static log =>
{
log.SetAttribute("attribute-key", "attribute-value");
return log;
});
});

options.MaxBreadcrumbs = 150; // Increasing from default 100
options.Release = "e386dfd"; // If not set here, SDK looks for it on main assembly's AssemblyInformationalVersion and AssemblyVersion
SentrySdk.ConfigureScope(s => s.SetTag("RootScope", "sent with all events"));

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
builder.AddSentry(options =>
{
// 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.SetBeforeSendLog(static log =>
{
log.SetAttribute("attribute-key", "attribute-value");
return log;
});

// Don't keep as a breadcrumb or send events for messages of level less than Critical with exception of type DivideByZeroException
options.AddLogEntryFilter((_, level, _, exception) => level < LogLevel.Critical && exception is DivideByZeroException);

options.ConfigureScope(s => s.SetTag("RootScope", "sent with all events"));
});
// Don't send logs for messages of level less than Warning for category Program
builder.AddFilter(typeof(Program).FullName, LogLevel.Warning);
Expand Down Expand Up @@ -86,8 +90,6 @@
Dependency.Work("8 - This unhandled exception is captured and includes Scope (A, B) and crumbs: (2, 4, 5) and event (3) ");
}

// Disposing the LoggerFactory will close the SDK since it was initialized through
// the integration while calling .Init()

internal static class Dependency
{
Expand Down
22 changes: 11 additions & 11 deletions samples/Sentry.Samples.OpenTelemetry.AzureFunctions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
var dsn = SamplesShared.Dsn;
#endif

// Initialise the Sentry SDK. The logging integration added below only forwards log messages to Sentry.
using var sentry = SentrySdk.Init(options =>
{
options.Dsn = dsn;
options.TracesSampleRate = 1.0;
options.UseOtlp(); // <-- Configure Sentry to use open telemetry
options.DisableSentryHttpMessageHandler = true; // So Sentry doesn't also create spans for outbound HTTP requests
options.Debug = true;
});

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
Expand All @@ -25,17 +35,7 @@
.AddHttpClientInstrumentation(); // From OpenTelemetry.Instrumentation.Http... adds automatic tracing for outgoing HTTP requests
});
})
.ConfigureLogging(logging =>
{
logging.AddSentry(options =>
{
options.Dsn = dsn;
options.TracesSampleRate = 1.0;
options.UseOtlp(); // <-- Configure Sentry to use open telemetry
options.DisableSentryHttpMessageHandler = true; // So Sentry doesn't also create spans for outbound HTTP requests
options.Debug = true;
});
})
.ConfigureLogging(logging => logging.AddSentry())
.Build();

await host.RunAsync();
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using Sentry;
using Sentry.AspNetCore.Blazor.WebAssembly.Internal;
using Sentry.Extensions.Logging;
using Sentry.Extensions.Logging.Extensions.DependencyInjection;
using Sentry.Infrastructure;
using Sentry.Internal;

// ReSharper disable once CheckNamespace - Discoverability
Expand All @@ -22,7 +25,15 @@ public static class WebAssemblyHostBuilderExtensions
/// <returns></returns>
public static WebAssemblyHostBuilder UseSentry(this WebAssemblyHostBuilder builder, Action<SentryBlazorOptions> configureOptions)
{
builder.Logging.AddSentry<SentryBlazorOptions>(blazorOptions =>
builder.Logging.AddSentryBlazor(configureOptions);
return builder;
}

internal static ILoggingBuilder AddSentryBlazor(this ILoggingBuilder logging, Action<SentryBlazorOptions> configureOptions)
{
logging.AddConfiguration();

logging.Services.Configure<SentryBlazorOptions>(blazorOptions =>
{
configureOptions(blazorOptions);

Expand All @@ -35,16 +46,27 @@ public static WebAssemblyHostBuilder UseSentry(this WebAssemblyHostBuilder build
blazorOptions.AddTransactionProcessor(new TraceIgnoreStatusCodeTransactionProcessor(blazorOptions));
});

builder.Services.AddSingleton<IConfigureOptions<SentryBlazorOptions>, BlazorWasmOptionsSetup>();
logging.Services.AddSingleton<IConfigureOptions<SentryBlazorOptions>, SentryHostOptionsSetup<SentryBlazorOptions>>();
logging.Services.AddSingleton<IConfigureOptions<SentryBlazorOptions>, BlazorWasmOptionsSetup>();

return builder;
logging.Services.AddSingleton<ILoggerProvider>(c => new SentryLoggerProvider(
c.GetRequiredService<IHub>(),
SystemClock.Clock,
c.GetRequiredService<IOptions<SentryBlazorOptions>>().Value.Logging));
logging.Services.AddSingleton<ILoggerProvider>(c => new SentryStructuredLoggerProvider(c.GetRequiredService<IHub>()));
logging.Services.AddSentry<SentryBlazorOptions>();

logging.AddFilter<SentryLoggerProvider>(_ => true);
logging.AddFilter<SentryStructuredLoggerProvider>("Sentry.ISentryClient", LogLevel.None);

return logging;
}
}

/// <summary>
/// Sentry Blazor Options
/// </summary>
public class SentryBlazorOptions : SentryLoggingOptions
public class SentryBlazorOptions : SentryHostOptions
{
// Awesome Blazor specific options go here
}
2 changes: 1 addition & 1 deletion src/Sentry.AspNetCore/BindableSentryAspNetCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Sentry.AspNetCore;

/// <inheritdoc cref="BindableSentryOptions"/>
internal class BindableSentryAspNetCoreOptions : BindableSentryLoggingOptions
internal class BindableSentryAspNetCoreOptions : BindableSentryHostOptions
{
public bool? IncludeActivityData { get; set; }
public RequestSize? MaxRequestBodySize { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Sentry.AspNetCore/SentryAspNetCoreLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ internal sealed class SentryAspNetCoreLoggerProvider : SentryLoggerProvider
/// Creates a new instance of <see cref="SentryAspNetCoreLoggerProvider"/>
/// </summary>
public SentryAspNetCoreLoggerProvider(IOptions<SentryAspNetCoreOptions> options, IHub hub)
: base(options, hub)
: base(hub, SystemClock.Clock, options.Value.Logging)
{
}

internal SentryAspNetCoreLoggerProvider(SentryAspNetCoreOptions options, IHub hub, ISystemClock clock)
: base(hub, clock, options)
: base(hub, clock, options.Logging)
{
}
}
2 changes: 1 addition & 1 deletion src/Sentry.AspNetCore/SentryAspNetCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sentry.AspNetCore;
/// An options class for the ASP.NET Core Sentry integration
/// </summary>
/// <inheritdoc />
public class SentryAspNetCoreOptions : SentryLoggingOptions
public class SentryAspNetCoreOptions : SentryHostOptions
{
/// <summary>
/// Gets or sets a value indicating whether [include System.Diagnostic.Activity data] to events.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry.Extensions.Logging;
using Sentry.Infrastructure;

Expand All @@ -11,13 +10,13 @@ namespace Sentry.AspNetCore;
[ProviderAlias("Sentry")]
internal sealed class SentryAspNetCoreStructuredLoggerProvider : SentryStructuredLoggerProvider
{
public SentryAspNetCoreStructuredLoggerProvider(IOptions<SentryAspNetCoreOptions> options, IHub hub)
: this(options.Value, hub, SystemClock.Clock, CreateSdkVersion())
public SentryAspNetCoreStructuredLoggerProvider(IHub hub)
: this(hub, SystemClock.Clock, CreateSdkVersion())
{
}

internal SentryAspNetCoreStructuredLoggerProvider(SentryAspNetCoreOptions options, IHub hub, ISystemClock clock, SdkVersion sdk)
: base(options, hub, clock, sdk)
internal SentryAspNetCoreStructuredLoggerProvider(IHub hub, ISystemClock clock, SdkVersion sdk)
: base(hub, clock, sdk)
{
}

Expand Down
17 changes: 17 additions & 0 deletions src/Sentry.Extensions.Logging/BindableSentryHostOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.Logging;

namespace Sentry.Extensions.Logging;

/// <inheritdoc cref="BindableSentryOptions"/>
internal class BindableSentryHostOptions : BindableSentryOptions
{
public LogLevel? MinimumBreadcrumbLevel { get; set; }
public LogLevel? MinimumEventLevel { get; set; }

public void ApplyTo(SentryHostOptions options)
{
base.ApplyTo(options);
options.MinimumBreadcrumbLevel = MinimumBreadcrumbLevel ?? options.MinimumBreadcrumbLevel;
options.MinimumEventLevel = MinimumEventLevel ?? options.MinimumEventLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

namespace Sentry.Extensions.Logging;

/// <inheritdoc cref="BindableSentryOptions"/>
internal class BindableSentryLoggingOptions : BindableSentryOptions
internal class BindableSentryLoggingOptions
{
public LogLevel? MinimumBreadcrumbLevel { get; set; }
public LogLevel? MinimumEventLevel { get; set; }
public bool? InitializeSdk { 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,41 @@ namespace Sentry.Extensions.Logging.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Sentry's services to the <see cref="IServiceCollection"/>
/// Adds Sentry's services to the <see cref="IServiceCollection"/>, initializing Sentry with
/// <typeparamref name="TOptions"/> when the hub is first resolved.
/// </summary>
/// <param name="services">The services.</param>
public static IServiceCollection AddSentry<TOptions>(this IServiceCollection services)
where TOptions : SentryLoggingOptions, new()
where TOptions : SentryHostOptions, new()
=> services.AddSentry<TOptions>(initializeSdk: true);

internal static IServiceCollection AddSentry<TOptions>(this IServiceCollection services, bool initializeSdk)
where TOptions : SentryHostOptions, new()
{
services.TryAddSingleton<SentryOptions>(
c => c.GetRequiredService<IOptions<TOptions>>().Value);

services.TryAddTransient<ISentryClient>(c => c.GetRequiredService<IHub>());
services.TryAddTransient(c => c.GetRequiredService<Func<IHub>>()());

services.TryAddSingleton<Func<IHub>>(c =>
if (initializeSdk)
{
var options = c.GetRequiredService<IOptions<TOptions>>().Value;

if (options.InitializeSdk)
services.TryAddSingleton<Func<IHub>>(c =>
{
var options = c.GetRequiredService<IOptions<TOptions>>().Value;
var hub = SentrySdk.InitHub(options);
SentrySdk.UseHub(hub);
}
options.ApplyConfigureScopeCallbacks(hub);

return () => HubAdapter.Instance;
});
}

return () => HubAdapter.Instance;
});
return services.AddSentryHub();
}

internal static IServiceCollection AddSentryHub(this IServiceCollection services)
{
services.TryAddTransient<ISentryClient>(c => c.GetRequiredService<IHub>());
services.TryAddTransient(c => c.GetRequiredService<Func<IHub>>()());
services.TryAddSingleton<Func<IHub>>(_ => () => HubAdapter.Instance);

// Custom handler for HttpClientFactory.
// Must be singleton: https://github.com/getsentry/sentry-dotnet/issues/785
Expand Down
Loading
Loading