serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.32k stars 209 forks source link

Sample.CustomPolicy, Sample was not found #327

Closed guimar86 closed 1 year ago

guimar86 commented 1 year ago

Description

Serilog is setup on a .net 6 web api project but once it starts it gives out the error message : "Sample.CustomPolicy, Sample was not found" and huge stack trace mainly refereing to the line of code below:

builder.Host.UseSerilog((context,configuration)=>configuration.ReadFrom.Configuration(context.Configuration));

Just for reference here is the entire code for the program class :

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IAppointmentService, AppointmentService>();
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json");
builder.Services.AddDbContext<SchedulingDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("SchedulingDb"));
});
builder.Host.UseSerilog((context,configuration)=>configuration.ReadFrom.Configuration(context.Configuration));
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseSerilogRequestLogging();
app.UseAuthorization();

app.MapControllers();
using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var context = services.GetRequiredService<SchedulingDbContext>();
    context.Database.Migrate();
}

app.Run();

Reproduction

Simply setup the nugets and try to run the app.

Expected behaviour

Web app should run normaly and should be able to see logs either on console or file.

Relevant package, tooling and runtime versions

-.net 6 web api project.

Additional context

Got this issue while following a tutorial here . Content for the appsettings.Development.json file came from serilog official documentation .

⚠️ As mentioned before the error shows on that specific line, however I am not sure whether the error is associated only to serilog.AspNetCore or to Serilog.Settings.Configuration or both. Hope this information helps, and thanks for the support.

nblumhardt commented 1 year ago

Hi! This isn't associated a bug, the config you've linked is only describing the syntax, not something you should copy verbatim into an application. Hopefully this is enough to unblock it for you, but if not, tagging a question serilog on Stack Overflow will get the right eyes on it. Cheers!