我只是想知道,如何使用 .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 = …Run Code Online (Sandbox Code Playgroud)