首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >worker服务中的事件日志未发布到Azure Application Insight

worker服务中的事件日志未发布到Azure Application Insight
EN

Stack Overflow用户
提问于 2021-01-05 00:09:34
回答 1查看 102关注 0票数 0

我正在构建一个Worker服务作为Windows服务..。我使用EventLog用于日志记录。我还想补充一点

ApplicationInsights来记录事件。我关注这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service,使用Nuget安装SDK,并按要求设置所有内容。这是我的program.cs配置。

代码语言:javascript
复制
public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)                
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService();
                services.AddApplicationInsightsTelemetryWorkerService();
                services.CustomeDependencyInjection();
            })
            .UseWindowsService()
            .ConfigureLogging(logging =>
            {
                logging.AddEventLog(eventLogSetting =>
                {
                    eventLogSetting.LogName = "MyTestEventLog";
                    eventLogSetting.SourceName = "MyTestEventApp";
                });
            });

下面是appsetting.json文件

代码语言:javascript
复制
{
  "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文件

代码语言:javascript
复制
public Worker(ILogger logger, TelemetryClient tc)
{
        _logger = logger;
        this.tc = tc;
 }
 public Task StartAsync(CancellationToken cancellationToken)
 {
        this._logger.LogInformation("In Start");
        using (tc.StartOperation("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什么?还是我找对地方了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-05 03:59:27

Application Insight默认为Warning作为日志级别。由于您使用level进行跟踪Information您需要使用appsettings.json文件配置应用程序洞察:

代码语言:javascript
复制
{
  "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"
      }
    }
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65565879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档