Search code examples
c#asp.net-core-2.1serilog

Serilog is not writing to MsSql server database and also not showing any error


I am trying to write log to MsSql server using serilog. My application is .Net Core 2.1 console Application.I am using Serilog MsSql sink

I have created a table using following sql command

CREATE TABLE [Logs] (

   [Id] int IDENTITY(1,1) NOT NULL,
   [Message] nvarchar(max) NULL,
   [MessageTemplate] nvarchar(max) NULL,
   [Level] nvarchar(128) NULL,
   [TimeStamp] datetimeoffset(7) NOT NULL,  -- use datetime for SQL Server pre-2008
   [Exception] nvarchar(max) NULL,
   [Properties] xml NULL

   CONSTRAINT [PK_Logs] 
     PRIMARY KEY CLUSTERED ([Id] ASC) 
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
           ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
     ON [PRIMARY]

) ON [PRIMARY];

My c# code:

static void Main(string[] args)
        {
            try
            {
                Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));


                Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Debug()
               .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
               .Enrich.FromLogContext()
              .WriteTo.MSSqlServer("Server=xyz;Database=abc;Trusted_Connection=True;", "Logs")
              .CreateLogger();

                Log.Information("Hello");


            }
            catch (Exception ex)
            {

                throw;
            }
        }

My problem is serilog doesn't write to database also It doesn't show any error. I tried to connect using Ado.Net it's connecting and inserting.But using serilog it doesn't work. What I am missing?


Solution

  • Only one Line solved my problem.

    Log.CloseAndFlush();