Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

736
Visualizações
How to turn off Serilog?

We are using Serilog to log items into a db with a Windows service, and the users wanted to be able to do a manual run, so we made a button (on a web page) to make a call to the same code (as a module, not the service itself).

When we added in the code to initialize the log so the code will continue adding to the db log table, it also logs all the http traffic after that as well. So after this code runs we want to 'turn off' the Logger running on the Webserver. Is there an easy way to do that?

Log.Logger = new LoggerConfiguration()
       .WriteTo.MSSqlServer(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString,
                "LOGS")
       .CreateLogger();
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Log levels can be modified at runtime with LoggingLevelSwitch:

var ls = new LoggingLevelSwitch();

Log.Logger = new LoggerConfiguration()
   .MinimumLevel.ControlledBy(ls)
   .WriteTo.MSSqlServer(...)
   .CreateLogger();

Logging will initially be at the Information level, you can change this via the switch.

Serilog doesn't define an Off level, but you can approximate it with:

ls.MinimumLevel = ((LogEventLevel) 1 + (int) LogEventLevel.Fatal);

...to turn logging off.

over 4 years ago · Santiago Trujillo Relatório

0

This question is a couple of years old, but I happen to be facing a similar problem right now.

The easiest way to turn Serilog's logging off is by creating a new logger without any sinks. From Serilog's documentation, Configuration Basics:

Log.Logger = new LoggerConfiguration().CreateLogger();
Log.Information("No one listens to me!");

Edit (08/05/2017)

The solution above is not correct. After some more research, I've found that this can be done by setting a filter when configuring the logger:

Log.Logger = new LoggerConfiguration()
    .Filter.ByExcluding(_ => !IsLoggingEnabled)
    .WriteTo.MSSqlServer(...)
    .CreateLogger();

, where IsLoggingEnabled is just a boolean flag.

over 4 years ago · Santiago Trujillo Relatório

0

according to Microsoft documentation for logging if you set the log level to None, all logs will be suppressed.

To suppress all logs, specify LogLevel.None. LogLevel.None has a value of 6, which is higher than LogLevel.Critical (5).

Any logs below the minimum level are not: Passed to the provider. Logged or displayed.

you can refer to this documentation under Configure logging section. logging for asp.net core

for Serilog you can set

"Serilog": {
"MinimumLevel": {
  "Default": "6"
}}

this configuration will turn off Serilog, I try it at run time and it worked.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda