为什么ASP.NET Core会记录所有内容? [英] Why asp.net core logging everything?
问题描述
我创建了我的第一个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.