-
-
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 extensions for IHostBuilder #190
Comments
True this is pending. Would be great to get some help on this. |
I'm migrating a console app over to the new generic host and in order to use var builder = Host.CreateDefaultBuilder()
.ConfigureLogging((ctx, loggingBuilder) =>
{
+ loggingBuilder.Services.Configure<SentryLoggingOptions>(ctx.Configuration.GetSection("Sentry"));
loggingBuilder.AddSentry();
}) To make this more reusable I've added a helper function: public static class SentryHostBuilderExtensions
{
public static IHostBuilder UseSentry(this IHostBuilder builder) =>
builder.ConfigureLogging((ctx, logging) =>
{
logging.Services.Configure<SentryLoggingOptions>(ctx.Configuration.GetSection("Sentry"));
logging.AddSentry();
});
} And then I use it just like the asp.net core integration: var builder = Host.CreateDefaultBuilder()
.UseSentry()
.ConfigureServices((ctx, services) => { ... }); |
This looks straightforward @xt0rted thanks. Is there a way to capture exceptions of |
@bruno-garcia I'm not seeing any graceful exception handling for these (here's the Host code). In the Here's a question about this and the feature has been moved to the backlog https://github.com/aspnet/Extensions/issues/813. |
I just tried the template for ASP.NET Core 3.0 on preview 9 which is supposed to be the last one (3.0 will be released in a week) and it looks like this:
So until we support Generic host, please just add:
and it should all work fine. |
Is there a way to configure Sentry release for the .NET core worker template? I generated a new project with .NET core 3.1 with public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
}); With the solution from @xt0rted I can add Sentry in general but I am unable to configure it. I would like to set the Release property for example. Edit: Nevermind, I found the solution: public static IHostBuilder UseSentry(this IHostBuilder builder) =>
builder.ConfigureLogging((context, logging) =>
{
IConfigurationSection section = context.Configuration.GetSection("Sentry");
logging.Services.Configure<SentryLoggingOptions>(section);
logging.AddSentry((c) =>
{
var version = Assembly
.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
c.Release = $"my-application@{version}";
});
}); |
@Kampfmoehre do you tag your assemblies? Like The docs are here: https://docs.sentry.io/platforms/dotnet/#automatically-discovering-release-version Code is: sentry-dotnet/src/Sentry/Reflection/AssemblyExtensions.cs Lines 22 to 31 in c777366
sentry-dotnet/src/Sentry/Internal/ApplicationVersionLocator.cs Lines 6 to 22 in eceab66
sentry-dotnet/src/Sentry/Internal/ReleaseLocator.cs Lines 11 to 13 in b770762
|
I don't want to go too much off topic here, but here is a little background: |
The snippet you added to the comment you edited is not needed because the SDK (code I pasted) does exactly the same. It looks for You can control these via |
Thank you @bruno-garcia for the info. So would I need to set AssemblyInformationalVersionAttribute to [email protected] for the release to be app@version, or is the my-application part taken from somewhere else? |
Correct. It'll take what's in either of those assemblies and set as Release. In the order I mentioned. |
@bruno-garcia Didn't work for me.. Using this I get an error saying I need to have a Startup class in my console app project. |
@ltechera Thanks for sharing that. This is something we could improve, adding to our backlog |
Supporting this! It is missing. |
Bump - running into issues with this. |
any updates here? |
Yes. I picked this up again a couple of weeks ago, starting with reviving #1015. There's a lot of work still to do, and I'm not sure exactly when it will be completed, but I will update this issue when it's ready. In the meantime, you can use the current release as follows: When using ASP.NET Core with the Web Host: WebHost.CreateDefaultBuilder()
.UseSentry(...); When using ASP.NET Core with the Generic Host: Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseSentry(...);
}); When using the Generic Host without ASP.NET Core: Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<YourHostedService>();
}
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.AddSentry(...);
}); The first two require |
I just hit this issue when trying to configure Sentry and Serilog inside an extension method for It would be nice if I can |
Bumping for support |
We haven't forgotten about this, there's just been a few other things prioritized higher. For now, use the workaround above. Thanks. |
@mattjohnsonpint Hurry up please, it's been almost 1 year. |
This comment was marked as resolved.
This comment was marked as resolved.
Hi again. Sorry, but this has been de-prioritized because the implementation is quite challenging, and there are already valid ways to register Sentry. As an example of the problem, consider that the Addressing your comment:
If your project is referencing ASP.NET Core, then you can use the If your project is not referencing ASP.NET Core, then you can't take advantage of Sentry's middleware anyway. You can initialize via
I'm not sure what you mean by that. Also, the code you gave just appears to be inlining the internals of the existing |
I'm using I have some other integrations such as When I called |
Can you give an example of the exception? Can you try Thanks. |
Also, I'm not sure why it matters that you've got other integrations. You certainly can use both var builder = WebApplication.CreateBuilder(args);
builder.Host.UseAutofac();
builder.WebHost.UseSentry();
var app = builder.Build(); Also, I haven't used Autofac in ages, but the docs tell me its actually |
@mattjohnsonpint |
Glad that worked for you. 🙂 Yeah, it's even more confusing if you take into account all the naming changes that have happened over the years between versions. It certainly is challenging for us to support them all, but we do our best to keep up! |
UpdateAfter much deliberation, trial, and error, we've decided not to do this. The main reason is that the In other words, Additionally, it is too difficult to provide different types of options objects to the configuration delegates. We need Guidance for ASP.NET CoreUse the var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(...); Related - we will also complete #2275 to make this clearer. Guidance for Generic Hosts / Worker ServicesUse the var hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.AddSentry(...);
}); Related - we will also complete #2274 to make this clearer. Thanks. |
Before you open the diff, a few paragraphs of explanation. For ASP.NET Core apps, there appear to be three disparate pathways to getting events onto sentry. - The first, and most direct one, is directly calling `SentrySdk.CaptureException()`. Generally does what you'd expect it to. - The second is via `IWebHostBuilder.UseSentry()`. This is an ASP.NET-ism that works by injecting a sentry-provided middleware into the request handling stack. Thus, all requests that fail due to a thrown exception will be reported to sentry, as the middleware will catch the error, log it, and throw it back up to the rest of the ASP.NET stack to handle. However, note that *this does not apply to background workers*, as they are generally outside of this entire flow. Even if we weren't hand-rolling them via singletons instantiated in `Startup`, and instead using `IHostedService` / `BackgroundService` which is the most correct ASP.NET-ism for that, for a proper persistent background service *you can't throw exceptions because you'd kill both the background service, and the entire server with it*. - Therefore, the third method, and the one officially recommended by the sentry team for background worker use cases (getsentry/sentry-dotnet#190 (comment)) is to use sentry's extension of `Sentry.Extensions.Logging`. Doing this will middleman all log calls to `Microsoft.Extensions.Logging` and push all errors (and warnings too, I believe) to sentry. In the context of all of the above, ppy#215 becomes doubly relevant; however, because that change requires more infra preparations and we probably want sentry logging on the replay upload process *now*, this is the minimal required change to make that happen. A side effect of this change is that the replay upload errors - which would be printed to stdout via `Console.WriteLine()` - will still be printed there, but using ASP.NET's default logging format, which is a little more... talkative, as the example below shows: fail: ScoreUploader[0] Error during score upload System.InvalidOperationException: nuh uh at osu.Server.Spectator.Storage.ThrowingScoreStorage.WriteAsync(Score score) in /home/dachb/Documents/opensource/osu-server-spectator/osu.Server.Spectator/Storage/ThrowingScoreStorage.cs:line 12 at osu.Server.Spectator.Hubs.ScoreUploader.Flush() in /home/dachb/Documents/opensource/osu-server-spectator/osu.Server.Spectator/Hubs/ScoreUploader.cs:line 117 Additionally, note that we have two *other* background workers like `ScoreUploader`, and they are: `MetadataBroadcaster` and `BuildUserCountUpdater`. I don't consider them anywhere as key as replay upload, therefore they are left as they are until we can get the full logging unification changes in.
Since this cost me some time to find, I want to add, when using HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddSentry(o =>
{
o.Dsn = "...";
}); |
The current implementation for MS's DI is for
IWebHostBuilder
, but given the new Generic Hosts, I think more and more will use theIHostBuilder
builder.Please add a
.UseSentry()
forIHostBuilder
. :)The text was updated successfully, but these errors were encountered: