简体   繁体   English

在 .NET Core 6.0 Razor 页面应用程序中配置 Serilog 的正确方法是什么?

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

I'm trying to configure Serilog to work with a .NET 6 Razor page web application.我正在尝试将 Serilog 配置为与 .NET 6 Razor 页面 web 应用程序一起使用。 I've tried following the aspnetcore directions here and applicationinsights here .我尝试按照此处的 aspnetcore 说明和此处applicationinsights 进行操作。 Nothing I try seems to work.我尝试的任何方法似乎都不起作用。 I see my enricher being hit but nothing is written to application insights.我看到我的浓缩器被击中,但没有写入应用程序洞察力。

The program.cs files looks like this: 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();

I've configured my appsettings.json file according to the Microsoft docs here , but threw in an InstrumentationKey as well just in case that would help.我已经根据此处的 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": "*"
}

I've been banging my head against this for 3 hours now and it just doesn't work.我一直在用头撞这个 3 个小时,但它不起作用。 I've been able to configure application insight WITHOUT Serilog in the application and it logged to the Application Insights resource, but I just can't get it to work WITH Serilog.我已经能够在应用程序中配置没有 Serilog 的应用程序洞察力,并且它已记录到 Application Insights 资源,但我无法让它与 Serilog 一起工作。

I suspect it has something to do with the instrumentation key not being read since I see error No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' when I uncomment the code in program.cs.我怀疑这与未读取检测密钥有关,因为当我取消注释 program.cs 中的代码时,我看到错误No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration'

Any help would be appreciated.任何帮助,将不胜感激。

  • Your Instrumentation key is not configured properly.您的 Instrumentation 密钥配置不正确。

Refer the below workaround请参考以下解决方法

  • Make sure you have below Nuget packages installed are updated确保您已更新安装以下 Nuget 软件包
Serilog.Settings.Configuration
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
Microsoft.ApplicationInsights.AspNetCore

Program.cs程序.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: Output:在此处输入图像描述

References taken from:参考资料来自:

Serilog.AspNetCore Serilog.AspNetCore

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .NET 6 Razor 页面中配置 Serilog - Configure Serilog in .NET 6 Razor Pages ASP.NET Core 6.0 - 最小 API:将 json 主体请求绑定到所需 class 的正确方法是什么? - ASP.NET Core 6.0 - Minimal APIs: What is the proper way to bind json body request to desired class? 对.NET Core Web应用程序进行单元测试的正确方法是什么? - What is the proper way to unit test .NET Core Web Application? 我可以在Razor页面配置或最小化CSS隔离捆绑文件吗? [ASP.NET 核心 6.0] - Can I configure or minimize CSS isolation bundled file in Razor Pages? [ASP.NET Core 6.0] 在EF Core中使用共享模式配置多租户的正确方法是什么 - What is the Proper Way To Configure a Multi Tenancy with Shared Schema in EF Core .NET Core 6.0:在 Visual Studio 2022 中添加新的脚手架 razor 页面时出错? - .NET Core 6.0 : error when adding new scaffolded razor page in Visual Studio 2022? 将ASP.Net核心Web应用程序编写的微服务部署到生产环境的正确方法是什么 - what is the proper way to deploy microservices written with ASP.Net core web application to production 确定应用程序位置的正确方法是什么? - What is the proper way to determine an application's location? 将 serilog 与 azure application insights 和 .Net core 结合使用 - Using serilog with azure application insights and .Net core 将.NET Core与EF结合使用时,用样本数据为大系统数据库播种的正确方法是什么? - What is the proper way to seed the big system's database with sample data when using .NET Core with EF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM