question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add extensions for IHostBuilder

See original GitHub issue

The current implementation for MS’s DI is for IWebHostBuilder, but given the new Generic Hosts, I think more and more will use the IHostBuilder builder.

Please add a .UseSentry() for IHostBuilder. 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:16
  • Comments:29 (16 by maintainers)

github_iconTop GitHub Comments

10reactions
xt0rtedcommented, Jul 29, 2019

I’m migrating a console app over to the new generic host and in order to use appsettings.json for my config with the ILoggingBuilder.AddSentry() method I needed to adjust my setup like so:

 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) => { ... });
4reactions
mattjohnsonpintcommented, May 19, 2022

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 Sentry.AspNetCore, but you only need Sentry.Extensions.Logging for the last one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create an IHostBuilder extension?
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, config) => { ...
Read more >
IHostBuilder Interface (Microsoft.Extensions.Hosting)
Extension Methods ; ConfigureHostOptions(IHostBuilder, Action<HostBuilderContext,HostOptions>). Adds a delegate for configuring the HostOptions of the IHost.
Read more >
How to add add Microsoft extensions hosting manually in . ...
How to add add Microsoft extensions hosting manually in . ... Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.
Read more >
The ASP.NET Core Generic Host: namespace clashes and ...
This extension method sets up the default configuration providers and logging providers for your app. The UseStartup<T>() extension sets the ...
Read more >
NServiceBus.Extensions.Hosting
Place UseNServiceBus() on the host builder before registering any hosted service (e.g. services.AddHostedService< ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found