Skip to content
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

Using both IInsights and ILogger #31

Closed
JohanO opened this issue Sep 26, 2024 · 2 comments
Closed

Using both IInsights and ILogger #31

JohanO opened this issue Sep 26, 2024 · 2 comments

Comments

@JohanO
Copy link

JohanO commented Sep 26, 2024

Today we have one method to register TinyInsight as the IInsights interface and another to register as ILogger, but how about a method to register both interfaces? Then we could use ILogger for common logging (and my project have a lot of code doing that), and use IInsights for setting userID, adding custom properties and all other ApplicationInsight specific things.

I have already added a method to do that in my app, but it would be nice to have it supported by the package.

My code:

        var provider = new ApplicationInsightsProvider(applicationInsightsConnectionString);
        configureProvider.Invoke(provider);
        provider.Initialize();

        var insights = new Insights();
        insights.AddProvider(provider);

        services
            .AddSingleton<IInsights>(insights)
            .AddSingleton<ILogger>(provider)
            .AddTransient<InsightsMessageHandler>();
@dhindrik
Copy link
Owner

I take a look at it.

@dhindrik
Copy link
Owner

dhindrik commented Oct 1, 2024

I added a GetProviders method so you can manually register it. I tried other approaches, but I found some problems with it.

You can now do like this

builder.Services.AddSingleton<ILogger>((serviceProvider) =>
        {
            var insights = serviceProvider.GetRequiredService<IInsights>();
            var providers = insights.GetProviders();

            if (providers.Any())
            {
                return (ILogger)providers.First();
            }

            throw new InvalidOperationException("No insights provider found");
        });

I also update the sample project with it.

@dhindrik dhindrik closed this as completed Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants