#azure #windows-services #azure-application-insights #asp.net-core-3.1 #event-log
#azure #windows-службы #azure-application-insights #asp.net-core-3.1 #журнал событий
Вопрос:
Я создаю рабочую службу как службу Windows. Я использую EventLog
для ведения журнала. Я также хочу добавить ApplicationInsights
в журнал событий. Я следую этой статье https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service , установите SDK с помощью Nuget и настройте все в соответствии с запросом. Вот моя program.cs
конфигурация.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddApplicationInsightsTelemetryWorkerService();
services.CustomeDependencyInjection();
})
.UseWindowsService()
.ConfigureLogging(logging =>
{
logging.AddEventLog(eventLogSetting =>
{
eventLogSetting.LogName = "MyTestEventLog";
eventLogSetting.SourceName = "MyTestEventApp";
});
});
Вот appsetting.json
файл
{
"ApplicationInsights": {
"InstrumentationKey": "bd******-****-****-****-***********b"
},
Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
Вот пример ведения журнала в worker.cs
файле
public Worker(ILogger<Worker> logger, TelemetryClient tc)
{
_logger = logger;
this.tc = tc;
}
public Task StartAsync(CancellationToken cancellationToken)
{
this._logger.LogInformation("In Start");
using (tc.StartOperation<RequestTelemetry>("Operation"))
{
/**
my service starting code
**/
this._logger.LogInformation("Service is being started");
tc.TrackEvent("Worker service starting operation completed.");
}
this._logger.LogInformation( "Service Started");
return Task.CompletedTask;
}
Когда я запускаю приложение, я вижу customEvent
. Я также вижу request
событие. Но я ничего не могу найти в том trace
, где я ожидал, что информация, отправленная с использованием _logger
. Я проверил окно вывода, а также не обнаружил, что какая-либо телеметрия отправляется против _logger.LogInformation
.
Что я здесь делаю не так и как я могу сделать информацию о регистраторе доступной для ApplicationInsights
? Или я не смотрю в нужное место?
Ответ №1:
Application Insight по умолчанию Warning
используется как уровень журнала. Поскольку вы выполняете трассировку с помощью level Information
, вам необходимо настроить application insights с помощью файла appsettings.json:
{
"ApplicationInsights": {
"InstrumentationKey": "bd******-****-****-****-***********b"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}