简体   繁体   中英

Logging with Serilog on .net core

I am working with Serilog for the first time and I read a couple of tutorials and it doesn't write on DB

this is my Program.cs file where I wrote the configurations:

public class Program
{
    public async static Task Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        await host.RunAsync();
    }



    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        var configSettings = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        Log.Logger = new Serilog.LoggerConfiguration()
            .ReadFrom.Configuration(configSettings)
            .CreateLogger();

        return Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(config =>
        {
            config.AddConfiguration(configSettings);
        })
        .ConfigureLogging(logging =>
        {
            logging.AddSerilog();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

    }
}

}

there is no problem while executing and it also created the table when missing but it is not inserting the logs.

"Serilog.Extensions.Logging": "1.0.0",
  "Serilog": {
    "MinimumLevel": "Error",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": constr,
          "tableName": "Logs",
          "autoCreateSqlTable": true
        }
      }
    ]
  }

this is the config in appjson.

and I consumed the log by adding it in the constructor and calling it on methods and that seems ok as well.

According to your description and settings, I found your Serilog MinimumLevel is error not information.

If you log the information it will not log into the database. You should use log error instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM