小编San*_*hna的帖子

如何使用 .NET Core 6 将日志详细信息写入文件

我只是想知道,如何使用 .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)

c# asp.net-core-webapi asp.net-core-6.0

7
推荐指数
2
解决办法
3万
查看次数

标签 统计

asp.net-core-6.0 ×1

asp.net-core-webapi ×1

c# ×1