Code: Select all
public static WebApplicationBuilder AddLogging(this WebApplicationBuilder builder, params ILogEventSink[] extraLogEventSinks)
{
builder.Host
.UseSerilog((context, services, configuration) =>
{
configuration
.ReadFrom.Configuration(context.Configuration)
.Enrich.WithMachineName()
.Enrich.WithEnvironmentName()
.Enrich.FromLogContext()
.Enrich.With()
.Enrich.With()
.Enrich.With(new ServiceNameEnricher(builder.Configuration))
.Enrich.WithExceptionDetails()
.Enrich.WithSensitiveDataMasking(options =>
{
options.MaskProperties = ["Authorization"];
options.MaskingOperators.Clear();
})
.Filter.With(new HealthEndpointFilter())
.WriteTo.ApplicationInsights(services.GetRequiredService(),
new OperationTelemetryConverter());
foreach (var extraLogEventSink in extraLogEventSinks)
{
configuration.WriteTo.Sink(extraLogEventSink);
}
var minimumLoglevel = builder.Environment.IsDevelopment() ? LogEventLevel.Verbose : LogEventLevel.Error;
if (builder.Environment.IsProduction())
{
configuration.WriteTo.Console(new CompactJsonFormatter(),
restrictedToMinimumLevel: minimumLoglevel);
}
else
{
configuration.WriteTo.Console(restrictedToMinimumLevel: minimumLoglevel);
}
});
return builder;
}
Code: Select all
.Services.AddSingleton()
Code: Select all
'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:
Serilog.Extensions.Hosting.DiagnosticContext Lifetime: Singleton ImplementationType: Serilog.Extensions.Hosting.DiagnosticContext':
Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'Serilog.Extensions.Hosting.DiagnosticContext'.)'
< /code>
und so diesen Fehler lösen, muss ich
hinzufügen.Services.AddSingleton().AddSingleton(Log.Logger)< /code> in die App,
wobei das Protokoll der serilog.log ist. Was wird passieren, wenn ich diesen Singleton -Logger hinzufüge, wird er andere Iloogger oder Iloogger
Code: Select all
using var log = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
< /code>
Ich bin mir nicht sicher, ob ich das hinzufügen sollen oder was ich habe, ist korrekt, aber der DI hat sich geändert? Ich habe eine andere Antwort irgendwo gesehen, dass nur das Hinzufügen der Verwendung von Serilog;