簡體   English   中英

ILogger 到 Application Insights

[英]ILogger to Application Insights

使用Microsoft.ApplicationInsights.AspNetCore v2.6.1 v2.6.1 和 .net core v2.2.2 我可以看到 Azure Application Insight Live Metric Stream 中的遙測數據,但我沒有看到我嘗試使用ILoggerStartup.cs記錄的條目Startup.cs或在控制器中。

我已經在Program.csStartup.cs嘗試了.UseApplicationInsights()WebHost.CreateDefaultBuilder ,但無濟於事。

services.AddApplicationInsightsTelemetry( options => {
  options.EnableDebugLogger = true;
});

我看到傳入的請求和請求失敗率,但沒有日志條目

this.logger.Log(LogLevel.Error, $"Test Error {Guid.NewGuid().ToString()}");
this.logger.LogTrace($"Test Trace {Guid.NewGuid().ToString()}");
this.logger.LogInformation($"Test Information {Guid.NewGuid().ToString()}");
this.logger.LogWarning($"Test Warning {Guid.NewGuid().ToString()}");
this.logger.LogCritical($"Test Critical {Guid.NewGuid().ToString()}");
this.logger.LogError($"Test Error{Guid.NewGuid().ToString()}");
this.logger.LogDebug($"Test Debug {Guid.NewGuid().ToString()}");

更新

如果您安裝了最新的軟件包Microsoft.Extensions.Logging.ApplicationInsights (2.9.1),您可以按照此文檔進行操作

在 program.cs 中:

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureLogging(logging=> {
                logging.AddApplicationInsights("your_insturmentation_key");
                logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace); #you can set the logLevel here
            });        
 }

然后在 controller.cs 中:

    public class HomeController : Controller
    {
        ILogger<HomeController> Logger { get; set; }
        TelemetryClient client = new TelemetryClient();
        public HomeController(ILogger<HomeController> logger)
        {
            this.Logger = logger;
        }

        public IActionResult Index()
        {
            Logger.LogTrace("0225 ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx");
            Logger.LogDebug("0225 ILogger: debug from index page aa111");
            Logger.LogInformation("0225 ILogger: infor from index page aa111");
            Logger.LogWarning("0225 ILogger: warning from index page aa111");
            Logger.Log(LogLevel.Error, "0225 ILogger: error from index page aa111");
            return View();
        }

        # other code

     }

測試結果(所有日志都發送到應用洞察):

在此處輸入圖片說明

請參閱帶有類似問題的問題以及我對此的回答: Log4Net和Application Insights-沒有數據通過

暫無
暫無

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

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