0

I am new to Serilog and I wrote a small test .NET Core app which tries to write a log to a local SQL Server database. The code below successfully creates the table, but no matter what I do I can't get it to write a log record. I didn't see any errors suggesting there are permission issues. Anyone have an idea?

using Serilog;
using Serilog.Sinks.MSSqlServer;

namespace SerilogTest
{
    class Program
    {


        private static ILogger InitSqlLogger()
        {
            var columnOptions = new ColumnOptions();
            columnOptions.Store.Remove(StandardColumn.MessageTemplate);
            columnOptions.Store.Remove(StandardColumn.Properties);
            columnOptions.Store.Remove(StandardColumn.Exception);
            columnOptions.Store.Remove(StandardColumn.Level);
            columnOptions.Store.Remove(StandardColumn.TimeStamp);
            return new LoggerConfiguration()
                .WriteTo.MSSqlServer(@"Server=(local);Database=LogEvents; Integrated Security = SSPI;", "Logs", autoCreateSqlTable: true, columnOptions: columnOptions)
                .CreateLogger();
        }
        private static void WriteLog()
        {
            var msg = "This is a {message:l}";
            Log.Logger.Information(msg, "test");
        }
        static void Main(string[] args)
        {
            Log.Logger = InitSqlLogger();
            WriteLog();
        }
    }
}

1 Answers1

1

Most likely, you're getting permission issues with writing to your SQL Server table.

Serilog sinks for logging are safe by default, and don't throw exceptions, so if you want to troubleshoot what's happening, you should look at Serilog's SelfLog

e.g.

Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91