我想知道如何在.NET 6中将日志详情保存到指定文件中。我使用Visual Studio 2022创建了一个新的ASP.NET Core 6 Web API项目。之前,我使用StartUp.cs
类来配置日志,但是在VS 2022中,已经没有了StartUp
类。
我应该如何在Program.cs
类中编写代码?
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<StudentDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("StudentDbContext")
?? throw new InvalidOperationException("Connection string 'StudentDbContext' not found.")));
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddScoped<IStudentRepository, StudentService>();
// Log Details (Using this path I want to save my all log details)
var path = Directory.GetCurrentDirectory();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();