Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog MSSqlServer sink not writing to table

I have the following statement in my Startup.cs:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.ColoredConsole()
    .WriteTo.MSSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=myDb.Logging;Trusted_Connection=True;", "Logs", autoCreateSqlTable: true)
    .WriteTo.RollingFile(pathFormat: Path.Combine(logPath, "Log-{Date}.txt"))
    .CreateLogger();

And in my Configure method:

loggerFactory.AddSerilog();

When I start the application, the table is created so I know the connection works. I get logged output to the console and to the file, however, no output to the database table.

What am I failing to do?

Other information: using asp.net core rc2-final, targeting net461, and using Serilog.Sinks.MSSqlServer 4.0.0-beta-100

like image 549
Ricky Hicks Avatar asked Jun 15 '16 23:06

Ricky Hicks


People also ask

Does Serilog support .NET Core?

Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets.

Can Serilog log database?

In order to proceed with database logging with Serilog in ASP.NET Core, the first step required is to install Serilog. Sinks. MSSqlServer package from NuGet. In the “Write To” subsection, we can set up the database connection string, along with the name of the table we want to create for logs.


1 Answers

At first glance it doesn't look like you're missing anything. It's likely that an exception is being thrown by the SQL Server Sink when trying to write to the table.

Have you tried checking the output from Serilog's self log?

Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

Update: Looks like a permission issue with you SQL Server/Local DB. This error message suggests the sink is trying to run an ALTER TABLE statement and the user running the application doesn't have permission to execute an ALTER TABLE statement.

Update 2: I suggest you write a simple Console App using the full .NET + Serilog v1.5.14 + Serilog.Sinks.MSSqlServer v3.0.98 to see if you get the same behavior... Just to rule out the possibility that there's a problem with the .NET Core implementation or with the beta sink version you're using

like image 54
C. Augusto Proiete Avatar answered Oct 02 '22 01:10

C. Augusto Proiete