【发布时间】:2023-01-09 14:59:18
【问题描述】:
我只想知道,如何使用 .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();
【问题讨论】:
-
documentation 说明了完成此操作必须做的事情。询问之前请务必检查文档!通读它,选择一种方法,尝试一下。如果它不起作用,您可以告诉我们您尝试了什么,并清楚地解释它为什么不起作用。
标签: c# asp.net-core-webapi asp.net-core-6.0