这是我的 .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.");
}
}