为什么asp.net核心记录所有内容?

问题描述 投票:0回答:1

我创建了第一个asp.net core 3.0应用程序。还有带有serilog文件扩展名的setup.cs文件,如下所示:

   public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            loggerFactory.AddFile("logs/ts-{Date}.txt");
            ....

而appsettings.json是:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

但是这会在应用程序运行时记录很多信息。但是我只想将日志调用保存在控制器操作或其他任何位置:

 public class WeatherForecastController : ControllerBase
 {
    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IEnumerable<WeatherForecast> Get()
    {
       _logger.LogInformation("oops");
       ....
    }
 }
asp.net-core logging serilog
1个回答
0
投票

。NET Core 3项目的默认模板还具有一个appsettings.Development.json文件,该文件将覆盖基本appsettings.json文件中的设置。它看起来像这样:

© www.soinside.com 2019 - 2024. All rights reserved.