问题
I am using Serilog.Extensions.Logging.File
to logs in file.
Here is my appsettings.json
file:
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "None",
"Microsoft": "None"
}
},
"LoggingFile": {
"IncludeScopes": false,
"pathFormat": "C:/logs/APILogs-{Date}.log",
"LogLevel": {
"Default": "Trace",
"System": "None",
"Microsoft": "None"
}
}
My Startup.cs code:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
IApiVersionDescriptionProvider provider,
ILoggerFactory loggerFactory)
{
// Removed other codes
loggerFactory.AddFile(Configuration.GetSection("LoggingFile"));
}
But It still log mvc request info such as below:
2019-09-21T13:28:59.6337460+05:30 80000019-0004-ff00-b63f-84710c7967bb [INF] Request starting HTTP/1.1 GET http://localhost:53534/api/values (ca22a1cb)
2019-09-21T13:28:59.8309629+05:30 80000019-0004-ff00-b63f-84710c7967bb [INF] Request finished in 202.16ms 200 (791a596a)
2019-09-21T13:29:00.1500727+05:30 8000001a-0004-ff00-b63f-84710c7967bb [INF] Request starting HTTP/1.1 GET http://localhost:53534/favicon.ico (ca22a1cb)
2019-09-21T13:29:00.2020227+05:30 8000001a-0004-ff00-b63f-84710c7967bb [INF] Request finished in 73.5631ms 200 (791a596a)
I don't to log these. It should only log when I want to log, such as in my contrller
_logger.LogInformation("Hello Info");
_logger.LogError("Hello error");
回答1:
Add following line of code to constructor of startup
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.MinimumLevel.Override("System", LogEventLevel.Error)
.Enrich.FromLogContext()
.WriteTo.RollingFile("C:/logs/APILogs-{Date}.log")
.CreateLogger();
And Then In confgure method, Add below line.
loggerFactory.AddSerilog();
For more info: Serilog
You can remove configuration from appsettings.
来源:https://stackoverflow.com/questions/58038428/turn-off-mvc-request-logging-in-asp-net-core-2-2-with-serilog