应用洞察-_logger.LogInformation

问题描述 投票:0回答:1

我创建了一个非常简单的 asp.net core 应用程序来了解有关 Azure Application Insights 的更多信息

我可以将 LogWarning、LogError 和 LogCritical 消息记录到我的 Azure Application Insights 中,当我查询日志下的“traces”表时,我会看到这些消息。我遇到的 LogDebug、LogInformation 和 LogTrace 问题我找不到消息

下面是一些相关代码/配置:

builder.Services.AddApplicationInsightsTelemetry();



// Example log level configuration in Startup.cs

builder.Services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));



"Logging": {

"LogLevel": {

"Default": "Information",

//Only the WARNING messages for HttpClient will be logged

"System.Net.Http.HttpClient": "Warning"

}

}

感谢任何帮助

azure asp.net-core azure-application-insights
1个回答
0
投票

我遇到的 LogDebug、LogInformation 和 LogTrace 问题我找不到消息

我在 Visual Studio 中使用 .net 版本 6.0 创建了简单的 dotnet core Web 应用程序。 我使用 appinsights 连接字符串在应用程序中配置应用程序洞察。

.cs 项目:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
    </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.22.0" />
  </ItemGroup>
</Project>

程序.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();
builder.Services.AddApplicationInsightsTelemetry();
//var abc = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

appsettings.json:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "Your-application insights-connection string"
  }
}

HomeController.cs:

using appinsightweb.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace appinsightweb.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

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

        public IActionResult Index()
        {
            _logger.LogInformation("This is an Information log.");
            _logger.LogWarning("This is a Warning log.");
            _logger.LogError("This is an Error log.");
            _logger.LogCritical("This is a Critical log.");

            // For Debug and Trace logs, ensure that the log level is configured appropriately in Startup.cs

            _logger.LogDebug("This is a Debug log.");
            _logger.LogTrace("This is a Trace log.");
            return View();
        }

通过使用上面的代码和配置能够在应用程序洞察中获取不同类型的日志。检查以下:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.