Turning off auto-logging by .Net while using Serilog.

huangapple go评论102阅读模式
英文:

Turning off auto-logging by .Net while using Serilog

问题

I'm using .Net Core 6 project and want to turn on only the logs that I'm explicitly logging with Serilog. I tried some approaches mentioned here but none of them seem to work, still I can see automatically generated logs by the .Net framework viz:

2023-06-29 19:45:05.866 +05:30 [INF] Now listening on: https://localhost:7164
2023-06-29 19:45:05.916 +05:30 [INF] Now listening on: http://localhost:5111
2023-06-29 19:45:05.925 +05:30 [INF] Application started. Press Ctrl+C to shut down.
2023-06-29 19:45:05.929 +05:30 [INF] Hosting environment: Development

Below are my 2 application files:

appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "None",
"System": "None",
"Microsoft": "None"
}
},
"AllowedHosts": "*"
}

Program.cs

using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add Serilog logger
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File($"Logs/log_{DateTime.Now:yyyyMMdd_HHmmss}.txt",outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();

// Add services to the container
builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

What am I doing wrong here?

英文:

I'm using .Net Core 6 project and want to turn on only the logs that I'm explicitly logging with Serilog. I tried some approaches mentioned here https://stackoverflow.com/questions/35251078/how-to-turn-off-the-logging-done-by-the-asp-net-core-framework but none of them seem to work, still I can see automatically generated logs by the .Net framework viz:

  1. 2023-06-29 19:45:05.866 +05:30 [INF] Now listening on: https://localhost:7164
  2. 2023-06-29 19:45:05.916 +05:30 [INF] Now listening on: http://localhost:5111
  3. 2023-06-29 19:45:05.925 +05:30 [INF] Application started. Press Ctrl+C to shut down.
  4. 2023-06-29 19:45:05.929 +05:30 [INF] Hosting environment: Development

Below are my 2 application files:

appsettings.json

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "None",
  5. "System": "None",
  6. "Microsoft": "None"
  7. }
  8. },
  9. "AllowedHosts": "*"
  10. }

Program.cs

  1. using Serilog;
  2. var builder = WebApplication.CreateBuilder(args);
  3. // Add Serilog logger
  4. Log.Logger = new LoggerConfiguration()
  5. .MinimumLevel.Information()
  6. .WriteTo.Console()
  7. .WriteTo.File($"Logs/log_{DateTime.Now:yyyyMMdd_HHmmss}.txt",outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
  8. .CreateLogger();
  9. // Add services to the container
  10. builder.Services.AddControllers();
  11. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  12. builder.Services.AddEndpointsApiExplorer();
  13. builder.Services.AddSwaggerGen();
  14. builder.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
  15. var app = builder.Build();
  16. // Configure the HTTP request pipeline.
  17. if (app.Environment.IsDevelopment())
  18. {
  19. app.UseSwagger();
  20. app.UseSwaggerUI();
  21. }
  22. app.UseHttpsRedirection();
  23. app.UseAuthorization();
  24. app.MapControllers();
  25. app.Run();

What am I doing wrong here?

答案1

得分: 1

这是Serilog日志记录。要关闭它,您需要覆盖您在Program.cs中设置Serilog配置的位置,如下所示:

  1. new LoggerConfiguration()
  2. .MinimumLevel.Information()
  3. .MinimumLevel.Override("Default", LogEventLevel.Fatal)
  4. .MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
  5. .MinimumLevel.Override("System", LogEventLevel.Fatal)

没有LogEventLevel.None,但'Fatal'应该可以消除大多数。您可以这样做:

  1. .MinimumLevel.Override("Default", LogEventLevel.Fatal + 1)

据说这可以抑制所有日志消息,包括致命错误,但我无法确认是否有效。

英文:

This is Serilog logging. To turn it off, you need to override where you set up your Serilog configuration, which you're doing in Program.cs:

  1. new LoggerConfiguration()
  2. .MinimumLevel.Information()
  3. .MinimumLevel.Override("Default", LogEventLevel.Fatal)
  4. .MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
  5. .MinimumLevel.Override("System", LogEventLevel.Fatal)

There isn't a LogEventLevel.None, but 'Fatal' should cut out most. You can do:

  1. .MinimumLevel.Override("Default", LogEventLevel.Fatal + 1)

which apparently works to suppress all log messages including fatal, but I can't confirm if this works.

huangapple
  • 本文由 发表于 2023年6月29日 22:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定