日志未被写入ApplicationInsights

3

这是我的 .net 6 program.cs 基础代码。在以下示例代码中,我试图将基本日志写入应用程序洞察。我在项目文件中引用了 Nuget 包。

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />

我正在使用经典的应用洞察。

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.ApplicationInsights;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

builder.Services?.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
    ConnectionString = "InstrumentationKey=19233009-a529-494c-9b4b-3fd57d363357;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/"

}); 
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthorization();
app.Logger.LogInformation("TEST LOGS");


app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

有什么建议,为什么我的应用洞察日志中没有记录“测试日志”?
更新
如@jasonc所建议的那样,我按照示例https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#example-programcs的说明进行操作,现在我的程序.cs文件中的日志已经被写入我的应用洞察中。但是我的控制器中的Ilogger日志仍未被记录。
我在我的控制器中有以下代码,但它没有被记录到应用洞察中。
  public class MyController : Controller
    {
        private readonly ILogger<MyController> _logger;
       

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

       
        public IActionResult Index()
        {
           
           _logger.LogInformation("From myController, running Index.");
         }

   }

https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger - jasonc
@jasonc,是的,我按照示例https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#example-programcs中的说明进行操作,程序.cs文件中记录了日志。但是我的控制器没有从Ilogger记录日志。 - roney
说实话,你控制器中的代码很好,这就是你注入日志的方式。你确认了你的路由是否正确,并且当你运行应用程序时,你的索引被正确调用了吗?你可以从一些简单的东西开始,比如将文本写到屏幕上,然后在其上添加日志记录。 - jasonc
1个回答

2

我可以通过以下更改在应用程序洞察中查看日志。

请检查以下解决方法:

  • 我们需要在appsettings.json的日志记录部分下添加ApplicationInsights

我的appsettings.json

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    //"InstrmentationKey": "YourInstrumentationKey",
    "ConnectionString": "Your Connection String from Application Insights"
  }
}
  • 您可以使用InstrumentationKeyConnectionString。但是建议使用ConnectionString

我的 Program.cs

using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

var app = builder.Build();

app.Logger.LogInformation("Configuring for {Environment} environment", app.Environment);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");  
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.Logger.LogInformation("TEST LOGS Final");
app.Logger.LogInformation("From Program, running the host now.");
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

My Controller

 private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            _logger.LogInformation("This is Controller, running Index.");
            return View();
        }

输出

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接