10

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

1 Answer 1

12

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

5
  • I'll try that right now - okay, so it says it's unable to write x log events to the database due to the following error: Cannot access destination table 'Logs' Jun 15, 2016 at 23:27
  • @CiaoProiete - That's a good idea. I was looking at the source for the sink, and it seems that an exception is being thrown when this line execute: await copy.WriteToServerAsync(_eventsTable, _token.Token).ConfigureAwait(false); I might also try to include the source in my project and step through it to find out what the issue is. Jun 16, 2016 at 0:10
  • 1
    @CiaoProiete - Decided to use MongoDb instead and that works quite well for logging. Thank you for your time. Jun 16, 2016 at 22:21
  • @CaioProiete I have the same issue but using mongoDb. When specify connection string with login and password it doesn't work
    – Pyrejkee
    Apr 29, 2019 at 12:42
  • 2
    Update: Serilog.Debbuging.SelfLog.Out has been replaced with Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); Reference: github.com/serilog/serilog/wiki/Debugging-and-Diagnostics
    – Yogi
    Oct 19, 2019 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.