WebApplicationBuilder is the setup object for an ASP.NET Core application. In Program.cs, call WebApplication.CreateBuilder(args), configure the builder, then call Build() to create a WebApplication. This delays creation of the application until its setup is complete. section 3.6
Even if you change nothing, the builder sets up configuration from JSON files and environment variables, logging, and services. Use builder.Services to register framework features and your own application services before Build(). Registered services can then be supplied to classes that depend on them through dependency injection. section 3.6 section 3.7.1
Add HTTP request logging
Call builder.Services.AddHttpLogging(...) to register and configure HTTP logging, and builder.Logging.AddFilter(...) to set its log level. Then call Build(). Add app.UseHttpLogging() to the resulting WebApplication when running in Development. The builder prepares the feature; the application adds its middleware to the request pipeline. section 3.7 section 3.7.1
Listing 3.4 The Program.cs file for a more complex example minimal API
using Microsoft.AspNetCore.HttpLogging;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(opts => #1
opts.LoggingFields = HttpLoggingFields.RequestProperties); #1
builder.Logging.AddFilter( #2
"Microsoft.AspNetCore.HttpLogging", LogLevel.Information); #2
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment()) #3
{
app.UseHttpLogging(); #4
}
app.MapGet("/", () => "Hello World!");
app.MapGet("/person", () => new Person("Andrew", "Lock")); #5
app.Run();
public record Person(string FirstName, string LastName); #6
The complete Program.cs example shows the order: create the builder, configure services and logging, build the application, add middleware and endpoints, then run it. Its /person endpoint returns a JSON representation of a Person record. section 3.7
Keep builder setup separate from WebApplication setup. After Build(), add middleware, map endpoints, and call Run() to start handling requests. The same sequence applies to Razor Pages: register the feature with builder.Services.AddRazorPages(), then map its pages on the WebApplication. section 3.8 section 13.1.2

