簡體   English   中英

在 .NET Core 6.0 Razor 頁面應用程序中配置 Serilog 的正確方法是什么?

[英]What's the proper way to configure Serilog in a .NET Core 6.0 Razor page application?

我正在嘗試將 Serilog 配置為與 .NET 6 Razor 頁面 web 應用程序一起使用。 我嘗試按照此處的 aspnetcore 說明和此處applicationinsights 進行操作。 我嘗試的任何方法似乎都不起作用。 我看到我的濃縮器被擊中,但沒有寫入應用程序洞察力。

program.cs 文件如下所示:

using AppInsightsSerilogCustomDimensions;
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.With<SystemInfoEnricher>()
    .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
    .CreateBootstrapLogger();

var builder = WebApplication.CreateBuilder(args);

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

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.With<SystemInfoEnricher>()
    .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces));

// Error: No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' has been registered.
//builder.Host.UseSerilog((context, services, configuration) => configuration
//    .ReadFrom.Configuration(context.Configuration)
//    .ReadFrom.Services(services)
//    .Enrich.With<SystemInfoEnricher>()
//    .WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces));


var app = builder.Build();


// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/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.MapRazorPages();

app.Run();

我已經根據此處的 Microsoft 文檔配置了我的 appsettings.json 文件,但也加入了 InstrumentationKey 以防萬一。

{
  "ApplicationInsights": {
    "InstrumentationKey": "--I put the key here--",
    "ConnectionString": "---I put a connection string here---"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Information"
    }
  },
  "AllowedHosts": "*"
}

我一直在用頭撞這個 3 個小時,但它不起作用。 我已經能夠在應用程序中配置沒有 Serilog 的應用程序洞察力,並且它已記錄到 Application Insights 資源,但我無法讓它與 Serilog 一起工作。

我懷疑這與未讀取檢測密鑰有關,因為當我取消注釋 program.cs 中的代碼時,我看到錯誤No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration'

任何幫助,將不勝感激。

  • 您的 Instrumentation 密鑰配置不正確。

請參考以下解決方法

  • 確保您已更新安裝以下 Nuget 軟件包
Serilog.Settings.Configuration
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
Microsoft.ApplicationInsights.AspNetCore

程序.cs

  public static void Main(string[] args)
    {      
        CreateHostBuilder(args).Build().Run();       
    }
  public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)                   
           .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                    .ReadFrom.Configuration(hostingContext.Configuration)
                  .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
             );   

Output:在此處輸入圖像描述

參考資料來自:

Serilog.AspNetCore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM