简体   繁体   English

ILogger 到 Application Insights

[英]ILogger to Application Insights

Using Microsoft.ApplicationInsights.AspNetCore v2.6.1 with .net core v2.2.2 I can see the telemetry going in Azure Application Insight Live Metric Stream but I don't see the entries which I'm trying to log with the ILogger inside the Startup.cs or in a controller.使用Microsoft.ApplicationInsights.AspNetCore v2.6.1 v2.6.1 和 .net core v2.2.2 我可以看到 Azure Application Insight Live Metric Stream 中的遥测数据,但我没有看到我尝试使用ILoggerStartup.cs记录的条目Startup.cs或在控制器中。

I've tried .UseApplicationInsights() with WebHost.CreateDefaultBuilder in Program.cs and also in Startup.cs like so, but to no avail.我已经在Program.csStartup.cs尝试了.UseApplicationInsights()WebHost.CreateDefaultBuilder ,但无济于事。

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

I see incoming requests and request failure rate but no log entries with我看到传入的请求和请求失败率,但没有日志条目

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()}");

update :更新

If you have the latest package Microsoft.Extensions.Logging.ApplicationInsights (2.9.1) installed, you can follow this doc .如果您安装了最新的软件包Microsoft.Extensions.Logging.ApplicationInsights (2.9.1),您可以按照此文档进行操作

In program.cs:在 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
            });        
 }

Then in the controller.cs:然后在 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

     }

The test result(all logs are sent to application insights):测试结果(所有日志都发送到应用洞察):

在此处输入图片说明

请参阅带有类似问题的问题以及我对此的回答: Log4Net和Application Insights-没有数据通过

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

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