工作进程服务中的事件日志未发布到Azure Application Insight [英] Event log in in worker service not published to Azure Application Insight

查看:0
本文介绍了工作进程服务中的事件日志未发布到Azure Application Insight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将工作服务构建为Windows服务。我使用EventLog来记录日志。我还想添加ApplicationInsights来记录事件。我遵循这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service,使用Nuget安装SDK,并按要求设置所有内容。这是我的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可以使用记录器信息?还是我找对地方了?

推荐答案

应用洞察默认为Warning作为日志级别。由于您是在Information级别进行跟踪,因此需要使用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"
      }
    }
  }
}

这篇关于工作进程服务中的事件日志未发布到Azure Application Insight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆