Search code examples
asp.net-corelogging.net-coreserilog

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.


Solution

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