-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IHostBuilder support #1015
Add IHostBuilder support #1015
Changes from all commits
7c052dd
efe7497
79e1ab5
7726e9a
ee2f2c2
8e518a2
7095a47
fc84525
8d3d3de
7fd6cd2
6638eca
06ca369
459325b
824e856
d3bc651
1bfce46
ded818b
f3dc8d4
cd4ea2f
885ace1
42a84ba
7c20f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,23 +17,23 @@ public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseSentry(o => | ||
webBuilder.UseStartup<Startup>(); | ||
}) | ||
.UseSentry(o => | ||
{ | ||
o.Debug = true; | ||
o.MaxRequestBodySize = RequestSize.Always; | ||
o.Dsn = "https://[email protected]/5428537"; | ||
o.TracesSampler = ctx => | ||
{ | ||
o.Debug = true; | ||
o.MaxRequestBodySize = RequestSize.Always; | ||
o.Dsn = "https://[email protected]/5428537"; | ||
o.TracesSampler = ctx => | ||
if (string.Equals(ctx.TryGetHttpRoute(), "/Home/Privacy", StringComparison.Ordinal)) | ||
{ | ||
if (string.Equals(ctx.TryGetHttpRoute(), "/Home/Privacy", StringComparison.Ordinal)) | ||
{ | ||
// Collect fewer traces for this page | ||
return 0.3; | ||
} | ||
// Collect fewer traces for this page | ||
return 0.3; | ||
} | ||
|
||
return 1; | ||
}; | ||
}); | ||
webBuilder.UseStartup<Startup>(); | ||
return 1; | ||
}; | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,15 +12,15 @@ | |||||
</ItemGroup> | ||||||
|
||||||
<ItemGroup> | ||||||
<ProjectReference Include="../../src/Sentry.Extensions.Logging/Sentry.Extensions.Logging.csproj" /> | ||||||
<ProjectReference Include="../../src/Sentry.AspNetCore/Sentry.AspNetCore.csproj" /> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to change the dependency to ASP.NET Core integration? The new types are all on the S.E.L, right?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New types are not on S.E.L. right now, see comment below |
||||||
</ItemGroup> | ||||||
|
||||||
<ItemGroup> | ||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" /> | ||||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.1" /> | ||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | ||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" /> | ||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.8" /> | ||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" /> | ||||||
</ItemGroup> | ||||||
|
||||||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Sentry.AspNetCore; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Extension methods to <see cref="IHostBuilder"/> | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class SentryHostBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry(this IHostBuilder builder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would've personally kept it in the asp.net core part, as it is the replacement of WebHostBuilder in the future. |
||
=> builder.UseSentry((Action<SentryAspNetCoreOptions>?)null); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="dsn">The DSN.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry(this IHostBuilder builder, string dsn) | ||
=> builder.UseSentry(o => o.Dsn = dsn); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureOptions">The configure options.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<SentryAspNetCoreOptions>? configureOptions) | ||
=> builder.UseSentry((_, options) => configureOptions?.Invoke(options)); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureOptions">The configure options.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<HostBuilderContext, SentryAspNetCoreOptions>? configureOptions) | ||
=> builder.UseSentry((context, sentryBuilder) => | ||
sentryBuilder.AddSentryOptions(options => configureOptions?.Invoke(context, options))); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureSentry">The Sentry builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<ISentryBuilder>? configureSentry) => | ||
builder.UseSentry((_, sentryBuilder) => configureSentry?.Invoke(sentryBuilder)); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureSentry">The Sentry builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<HostBuilderContext, ISentryBuilder>? configureSentry) | ||
{ | ||
// The earliest we can hook the SDK initialization code with the framework | ||
// Initialization happens at a later time depending if the default MEL backend is enabled or not. | ||
// In case the logging backend was replaced, init happens later, at the StartupFilter | ||
_ = builder.ConfigureLogging((context, logging) => | ||
bruno-garcia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var sentryBuilder = logging.AddSentry(context.Configuration); | ||
configureSentry?.Invoke(context, sentryBuilder); | ||
}); | ||
|
||
_ = builder.ConfigureServices( s=> | ||
{ | ||
_ = s.AddSentryStartupFilter(); | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!