繁体   English   中英

工作人员服务中的事件登录未发布到 Azure Application Insight

[英]Event log in in worker service not published to Azure Application Insight

我正在构建一个Worker Service作为Windows Service 我使用EventLog进行日志记录。 我还想添加ApplicationInsights来记录事件。 我按照这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service ,安装 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事件。 但是我在我期望使用_logger发送的信息的trace中找不到任何内容。 我检查了 output window 也没有发现针对_logger.LogInformation发送任何遥测。

我在这里做错了什么,如何使 Logger Info 可用于ApplicationInsights 还是我没有找对地方?

Application Insight 默认将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"
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM