Serilog MSSqlServer sink not writing to table
Asked Answered
V

1

11

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

Vitiated answered 15/6, 2016 at 23:6 Comment(0)
L
13

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

Lewan answered 15/6, 2016 at 23:26 Comment(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'Vitiated
@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.Vitiated
@CiaoProiete - Decided to use MongoDb instead and that works quite well for logging. Thank you for your time.Vitiated
@CaioProiete I have the same issue but using mongoDb. When specify connection string with login and password it doesn't workVaticide
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-DiagnosticsHeirloom

© 2022 - 2024 — McMap. All rights reserved.