[英]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.任何帮助,将不胜感激。
Refer the below workaround请参考以下解决方法
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:参考资料来自:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.