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

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

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:

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?

答案1

得分: 1

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

new LoggerConfiguration()
    .MinimumLevel.Information()
    .MinimumLevel.Override("Default", LogEventLevel.Fatal)
    .MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
    .MinimumLevel.Override("System", LogEventLevel.Fatal)

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

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

        new LoggerConfiguration()
           .MinimumLevel.Information()
           .MinimumLevel.Override("Default", LogEventLevel.Fatal)
           .MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
           .MinimumLevel.Override("System", LogEventLevel.Fatal)

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

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

确定