Как регистрировать данные в консоли / выводе веб-задания и в службе приложений Azure с использованием приложений - PullRequest
0 голосов
/ 10 июля 2019

Я использую веб-задания v3 с HostBuilder.

Как мне настроить регистратор, чтобы я мог использовать telemetryClient для отслеживания данных, которые видны на выходе консоли И видны в службе приложений Azure эта веб-работа запущена?

Я хотел бы получить экземпляр ILogger, который регистрируется в консоли AND azure.

эта строка кода:

 var logger = host.Services.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("I log only in the console but not azure.");

НЕ отправляет свои данные в диаграммы анализа приложений Azure ...

Я НЕ хочу использовать ILogger И телеметрический клиент для записи глупостей.

var builder = new HostBuilder()
                .UseEnvironment("Development")
                .ConfigureWebJobs(b =>
                {
                    // Add extensions and other WebJobs services
                })
                .ConfigureAppConfiguration(b =>
                {

                    // Add configuration sources          
                })
                .ConfigureLogging((context, b) =>
                {
                    // Add Logging Providers
                    b.AddConsole(); 


                    // If this key exists in any config, use it to enable App Insights
                    string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]; // taken from the appservice environmentvariable

                    appInsightsKey = "xxxxxxxx";

                    if (!string.IsNullOrEmpty(appInsightsKey))
                    {
                        // This uses the options callback to explicitly set the instrumentation key.
                        b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                    }
                })
                .UseConsoleLifetime();

            var host = builder.Build();

            // my code like telemetryClient.TrackTrace("show this text in console output AND azure app service where this webjob belongs to")

            using (host)
            {
                host.Run();
            }

1 Ответ

0 голосов
/ 16 июля 2019

Пожалуйста, исправьте меня, если я вас неправильно понял.

Для "Я хотел бы получить экземпляр ILogger, который регистрируется в консоли И лазури", я использую код ниже:

        static void Main(string[] args)
        {
            var builder = new HostBuilder()
                .ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageCoreServices();
                    b.AddAzureStorage();
                })
                .ConfigureAppConfiguration(b =>
                {

                }
                )
                .ConfigureLogging((context, b) =>
                {

                    b.AddConsole();
                    string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                    if (!string.IsNullOrEmpty(instrumentationKey))
                    {
                        b.AddApplicationInsights(o => o.InstrumentationKey = instrumentationKey);
                    }
                })                
                .UseConsoleLifetime();

            var host = builder.Build();

            var logger = host.Services.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("hello 111111111111111");

            using (host)
            {
                host.Run();
            }
        }

После выполнения кода сообщение регистрируется в консоли и в приложении анализируются на портале Azure:

enter image description here

...