在 Visual Studio 中记录 DefaultAzureCredential

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

我正在尝试在 Visual Studio 中为 DefaultAzureCredential 进行日志记录。我设置了日志记录,但是当我获得 DefaultAzureCredential 令牌时,Visual Studio 控制台中没有输出任何日志记录。

这是我的程序.cs

private static void Main(string[] args)
 {
     using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
     var builder = WebApplication.CreateBuilder(args);
     builder.Logging.AddAzureWebAppDiagnostics();
     builder.Logging.AddConsole();

     // Add services to the container.
     builder.Services.AddControllersWithViews();

     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.MapControllerRoute(
         name: "default",
         pattern: "{controller=Home}/{action=Index}/{id?}");

     app.Run();
 }

这是我获取令牌的方式

     DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions
  {
      Diagnostics =
      {
          LoggedHeaderNames = { "x-ms-request-id" },
          LoggedQueryParameters = { "api-version" },
          IsLoggingContentEnabled = true,
          IsLoggingEnabled = true,
          IsAccountIdentifierLoggingEnabled = true
      },
      ExcludeManagedIdentityCredential = true
  };

  var credential = new DefaultAzureCredential(options); 
  
  AccessToken token = credential.GetToken(new Azure.Core.TokenRequestContext(new[] { "https://database.windows.net/.default" }));

关于为什么这不起作用有什么建议吗?

azure logging azure-sdk-.net
2个回答
0
投票

我设置了日志记录,但是当我获得 DefaultAzureCredential 令牌时,Visual Studio 控制台中没有输出任何日志记录。

首先,检查您是否已添加用于日志记录所需的 NuGet 包。使用

Microsoft.Extensions.Logging.Console
包登录到控制台。

  • 然后,在
    Program.cs
    文件中,配置日志记录以包括 Azure.Identity 日志。将
    Azure.Identity
    命名空间的日志级别设置为
    Information
    Debug

代码:

using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

// Configure logging
builder.Logging.AddConsole(); // Add logging to the console

// Set logging level for Azure.Identity namespace
builder.Logging.SetMinimumLevel(LogLevel.Debug); // or LogLevel.Information

// Configure Application Insights telemetry.
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["ApplicationInsights:InstrumentationKey"]);

builder.Services.AddControllersWithViews(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages()
    .AddMicrosoftIdentityUI();

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.UseAuthentication();
app.UseAuthorization();

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

app.Run();

交易搜索:

enter image description here

认证日志: enter image description here


0
投票

Azure SDK 包将日志发送到 ETW。您已经连接了

AzureEventSourceListener
,它将捕获它们,但您并没有将这些捕获的日志写入任何地方。例如,要将它们发送到控制台,您需要创建一个控制台记录器:

using AzureEventSourceListener listener = 
    AzureEventSourceListener.CreateConsoleLogger();

要与

ILogger
和 ASP.NET 日志记录基础结构集成,最简单的方法是使用
AzureEventSourceLogForwarder
包中的
Microsoft.Extensions.Azure
,它会隐式启动侦听器并将捕获的日志写入到
ILogger
。看起来像:

using Azure.Identity;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.TryAddSingleton<AzureEventSourceLogForwarder>();

更多讨论和示例可以在使用适用于 .NET 的 Azure SDK 进行日志记录中找到。

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