microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

How to use my own TelemetryConfiguration? #269

Closed OskarKlintrot closed 5 years ago

OskarKlintrot commented 5 years ago

I want to use my own TelemetryConfiguration (I use code to configure AI, not the config-file, hence I want to create and inject my own config) but I can't figure out how.

I tried to use "override" the registered TelemetryConfig like this:

loggerBuilder.AddApplicationInsights();

loggerBuilder.Services.Configure<TelemetryConfiguration>(telemetryConfiguration);

where telemetryConfiguration is a local method but it didn't do anything from what I can tell. I'm trying to get it working on .NET Framework 4.6.1.

RamjotSingh commented 5 years ago

Directly configuring TelemetryConfiguration should work (including your way). However the customized TelemetryConfiguration will not be available during ApplicationStartup. Post that ILogger will be sharing the same configuration that your rest of the application is using. Can you be a little more specific as to what exactly is not working and at which stage (appStartup e.g. ConfigureServices and Configure vs request execution).

OskarKlintrot commented 5 years ago

Right now I try to get it working in a web job (net461 console app, basically). I have an extension method that looks something like this:

public static IServiceCollection AddLoggingForWebJob(this IServiceCollection services, bool developerMode)
{
    return services
        .AddLogging(loggerBuilder =>
        {
            var logLevel = developerMode ? LogLevel.Trace : LogLevel.Debug;
            var assembly = Assembly.GetEntryAssembly();

            loggerBuilder.SetMinimumLevel(logLevel);

            loggerBuilder.AddDebug();

            loggerBuilder.Services.Configure<TelemetryConfiguration>(telemetryConfigurationAction);

            loggerBuilder.AddApplicationInsights();

            loggerBuilder.AddFilter<ApplicationInsightsLoggerProvider>(null, logLevel);

            void telemetryConfigurationAction(TelemetryConfiguration telemetryConfiguration)
            {
                TelemetryConfigurationSetup
                    .InitializeTelemetryConfigurationBuilder(assembly, developerMode, telemetryConfiguration)
                    .BuildTelemetryConfiguration();
            }
    });
}

I call it directly from Main().

OskarKlintrot commented 5 years ago

I'm using the TelemetryClient in other places to track dependencies etc but when I used my TelemetryConfigurationSetup to get a new TelemetryConfiguration-object that I used to create a new TelemetryClient (new TelemetryClient(telemetryConfiguration)) it didn't work there either. I got desperate and just passed the TelemetryConfiguration.Active as the argument (not here, when setting up other stuff) to InitializeTelemetryConfigurationBuilder (the parameter is nullable, if null I use TelemetryConfiguration.CreateDefault() and return that) and it all started to work, even the ApplicationInsightsProvider that didn't work previously but now it did. I can't even find in the source code that it would use TelemetryConfiguration.Active? Anyway, it works fine now on Azure as well (just had to manually provide the instrumentation key). Not sure what solved what and what I can undo and still have it working but that seems to be a PEBKAC-issue and nothing else.