簡體   English   中英

AppInsights 僅保存遙測數據,但不使用托管在 IIS 上的 .Net Core web 應用程序保存日志

[英]AppInsights is only saving telemetry data but not saving logs with a .Net Core web app hosted on an IIS

我的Program class 看起來像這樣

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args)
            .Build()
            .Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((webHostingBuilder, configBuilder) =>
            {
                configBuilder.SetBasePath(webHostingBuilder.HostingEnvironment.ContentRootPath)
                                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                .AddJsonFile($"appsettings.{webHostingBuilder.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                                .AddEnvironmentVariables();
            })
        .ConfigureLogging((webHostingBuilder, logging) =>
        {
            logging.ClearProviders();
            logging.AddConsole();
            logging.AddDebug();
            logging.AddApplicationInsights(
                webHostingBuilder.Configuration.GetValue<string>("Logging:ApplicationInsights:InstrumentationKey"));
        })
        .UseStartup<Startup>();
}

在我的ConfigureServicesStartup

services.AddApplicationInsightsTelemetry(configuration["Logging:ApplicationInsights:InstrumentationKey"]);

這是我的 controller class:

public class AccountController : BaseController
{
    private ILogger _logger;

    public AccountController(ILogger<AccountController> logger)
    {   ... } 

    [HttpGet]
    public async Task<IActionResult> Login(string returnUrl)
    {
        _logger.LogError("SuperError");
    }
}

為了保存日志,我還需要配置其他什么嗎? 在做我的例子時,我在這里看不到任何東西

在此處輸入圖像描述

你的代碼看起來不錯。 有時,日志可能需要一段時間才能顯示在App Insights中。 給出正確InstrumentationKey的示例配置:

https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#im-using-the-standalone-package-microsoftextensionsloggingapplicationinsights-and-enabling-application-insights-provider-by-calling- builderaddapplicationinsightsikey-is-there-an-option-to-get-an-instrumentation-key-from-configuration

啟動.cs

services.AddApplicationInsightsTelemetry();

appsettings.json

"ApplicationInsights": {
  "InstrumentationKey": "putinstrumentationkeyhere"
}

WeatherForecastController.cs

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
    _logger = logger;
}

[HttpGet]
public ActionResult<IEnumerable<WeatherForecast>> Get()
{
    _logger.LogInformation("WeatherForecastController Get");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM