为什么ASP.NET Core会记录所有内容? [英] Why asp.net core logging everything?

查看:73
本文介绍了为什么ASP.NET Core会记录所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have creted my first asp.net core 3.0 application. And setup.cs file with serilog file extension like following:

   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是:

And appsettings.json is:

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

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

But this logs so many information while application running. But I want to save only my log calls in controller actions or anywhere else:

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

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

    [HttpGet]
    public IEnumerable<WeatherForecast> Get()
    {
       _logger.LogInformation("oops");
       ....
    }
 }

推荐答案

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

The default template for .NET Core 3 projects also has a appsettings.Development.json file which overrides settings in the base appsettings.json file. It will look something like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

请注意,日志级别设置为 Debug ,这就是您的日志包含如此多条目的原因.

Note that the log level is set to Debug which is why your logs contain so many entries.

使用 CreateDefaultBuilder 初始化新的主机生成器时,会自动调用

AddJsonFile 两次.调用该方法可从以下位置加载配置:

AddJsonFile is automatically called twice when you initialize a new host builder with CreateDefaultBuilder. The method is called to load configuration from:

  • appsettings.json –首先读取此文件.该文件的环境版本可以覆盖appsettings.json文件提供的值.
  • appsettings.{Environment} .json –基于IHostingEnvironment.EnvironmentName加载文件的环境版本.
  • appsettings.json – This file is read first. The environment version of the file can override the values provided by the appsettings.json file.
  • appsettings.{Environment}.json – The environment version of the file is loaded based on the IHostingEnvironment.EnvironmentName.

这篇关于为什么ASP.NET Core会记录所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆