.NET Core 3.0工作者-无法使日志与Application Insights或EventLog一起使用 更新更新2 更新3

我需要.NET 3.0辅助服务才能登录到Azure Application Insights和EventLog。这些都不起作用(几乎)!

这是我的CreateHostBuilder

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext,services) =>
                {
                    IConfiguration configuration = hostContext.Configuration;

                    WatchdogOptions options = configuration.GetSection("WorkerOptions").Get<WatchdogOptions>();
                    if (options == null) throw new WatchdogException("WorkerOptions settings are not set");

                    services.AddSingleton(options);

                    services.AddHostedService<Worker>()
                    // .AddApplicationInsightsTelemetryWorkerService();
                    ;
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    //logging.AddConsole();
                    logging.AddApplicationInsights("<instr-key>");
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",LogName = "Watchdog"
                    });
                });
        }

1)无论我做什么,EventLog都没有来自我的工作人员的任何记录。我确实在应用程序设置中设置了日志记录级别:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug","System": "Information","microsoft": "Information"
    }
  },"EventLog": {
    "LogLevel": {
      "Default": "Information","microsoft.Hosting.Lifetime": "Information"
    }
  }
}

2)应用洞察力仅在注释.AddApplicationInsightsTelemetryWorkerService() 并且将检测键硬编码在logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7")中时获得记录。应该怎么做才能从应用程序设置中获取密钥?

3)为什么这么麻烦?

更新

完整的app.development.settings

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug","microsoft": "Information"
    },"EventLog": {
      "LogLevel": {
        "Default": "Information","microsoft.Hosting.Lifetime": "Information"
      }
    }
  },"ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-what-ever-0000000000"
  }
}

更新2

ApplicationInsights 已添加到记录部分:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug","microsoft.Hosting.Lifetime": "Information"
      }
    },"ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    } 
  },"ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

更新3

Logging:ApplicationInsights:LogLevel的属性名称已更改:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug","ApplicationInsights": {
      "LogLevel": {
        "PushNotificationsWatchdog.Worker": "Information"
      }
    }
  },"ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

同一件事-App Insights中没有记录。仪表键正确。

解决方案

感谢@ peter-bons!

因此,我更改了ConfigureServices()ConfigureLogging()的顺序,并使用了UPDATE 2中的appsettings,它现在可以工作了!这样我们就可以了:

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",LogName = "Watchdog"
                    });
                })
                .ConfigureServices((hostContext,services) =>
                {
                    services.AddHostedService<Worker>()
                        .AddApplicationInsightsTelemetryWorkerService();
                })
                .UseWindowsService(); // windows only feature
        }
lijunjiji 回答:.NET Core 3.0工作者-无法使日志与Application Insights或EventLog一起使用 更新更新2 更新3

直到有“ Application Insights Telemetry WorkerService”之类的东西:-)

我使用AddApplicationInsightsTelemetryWorkerService使它工作。但是只有在您不致电logging.ClearProviders();或在ConfigureLogging之前致电ConfigureServices的情况下,否则添加的日志记录提供程序将被清除。

这有效:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                })
                .ConfigureServices((hostContext,services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddApplicationInsightsTelemetryWorkerService();
                });

使用此配置:

  "Logging": {
    "LogLevel": {
      "Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"
    },"ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  },"ApplicationInsights": {
      "InstrumentationKey": "xxx"
    }
  }

正如您在输出中看到的那样,正确提取了AI密钥:

Application Insights Telemetry: {
    "name": "Microsoft.ApplicationInsights.Dev.3b40adb096064da0816e7b8579aa443c.Message","time": "2019-11-13T07:52:11.0027057Z","iKey": "xxx","tags": {
        "ai.application.ver": "1.0.0.0","ai.cloud.roleInstance": "xxx","ai.internal.sdkVersion": "il:2.11.0-21511","ai.internal.nodeName": "xxx"
    },"data": {
        "baseType": "MessageData","baseData": {
            "ver": 2,"message": "Application started. Press Ctrl+C to shut down.","severityLevel": "Information","properties": {
                "{OriginalFormat}": "Application started. Press Ctrl+C to shut down.","CategoryName": "Microsoft.Hosting.Lifetime","DeveloperMode": "true"
            }
        }
    }
}
,

AddApplicationInsightsTelemetryWorkerService()是在WorkerService中启用应用程序见解的推荐方法。它可以从appsettings.json获取ikey。这将在内部设置日志记录提供程序(applicationinsightslogging提供程序)。

ApplicationInsights日志记录提供程序默认仅捕获“警告”级别或更高级别的日志(https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs)。这是我的第一个怀疑。您是否可以记录一些警告或错误信息并查看是否被捕获。 或更改应用程序见解捕获的默认级别,以同时捕获信息或跟踪级别。 (https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level

本文链接:https://www.f2er.com/3128044.html

大家都在问