0

Help

Why can't I get serilog to log to sqlserver I have used forever to get this to work

It logs fine to the console I have installed the nuget packages

Serilog Serilog.Sinks.Console Serilog.Sinks.MSSqlServer

//First a quick test if the connection string works (db can open, and if the table exists, it works but no rows


var connection = new ConnectionStringSettings();
connection.ConnectionString = "Data Source=server-name;Initial Catalog=db-name;Persist Security Info=True;User ID=user-name;Password='password-name'";
var sqlc = new SqlConnection(connection.ConnectionString);
string query = "select * from LogEvents";

sqlc.Open();
using (var command = new SqlCommand(query, sqlc))
{
    var reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
        }
    }
    reader.Close();
}
sqlc.Close();

var log = new LoggerConfiguration()
.WriteTo
.MSSqlServer(
connectionString: connection.ConnectionString,
sinkOptions: new MSSqlServerSinkOptions { TableName = "LogEvents", AutoCreateSqlTable = true }
)
.WriteTo.Console()
.CreateLogger();

Log.Logger = log;
Log.Information("123");
Log.CloseAndFlush();
Console.WriteLine("Hello, World!");
Aj.son
  • 13
  • 5

0 Answers0