9

I'm actually trying to troubleshoot a problem where the MSSqlServer sink isn't writing anything to my database tables. I was advised to hook up the SelfLog to see what was going on but it isn't actually outputting anything and I'm not sure if I'm missing something. This is the setup method:

public SerilogLogger()
{
    Serilog.Debugging.SelfLog.Enable(
        msg => System.Diagnostics.Trace.WriteLine(msg));

    _logger = new LoggerConfiguration()
        .WriteTo.MSSqlServer(@"Server=<MyConnectionString>", "Logs")
        .WriteTo.Trace()
        .CreateLogger();
}

The methods in this class are just calling log methods on the _logger which is set up here. The logs that I'm creating are being written to the output window via the trace sink, but nothing goes to the database and Serilog isn't outputting anything let alone any errors. I even tried deliberately messing up the connection string just to get it to output anything and no dice. Any thoughts?

JDC
  • 3,078
  • 1
  • 17
  • 15
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176
  • 1
    I wonder if the logging level is too high. Try setting the minimum logging level to Verbose (`.MinimumLevel.Verbose()`). – PatrickSteele Sep 21 '16 at 22:04
  • @Sinaesthetic Have you found out an answer? – netaholic Jul 03 '17 at 15:24
  • @netaholic Sorry, but I don't think I ever did. I ended up just switching off of MSSql as a sink and using Loggly instead without much issue. – Sinaesthetic Jul 04 '17 at 18:37
  • 1
    In my case the issue was that number of parameters didn't match the number of passed objects into log method. Another issue was that `Serilog.Debugging.SelfLog.Enable` didn't fire right after an error, and my console app was shutting down before it would hit a breakpoint – netaholic Jul 10 '17 at 14:30
  • @patricksteele I'm sure that was probably the problem. The logging level defaults to Information and I think most things were debug at the time. I ran into the same problem again and most everything was set to verbose, but there appears to be a hierarchy involved where the logger is configured separately of the sinks, so I had the sinks configured to verbose but the logger was at info, so no verbose or debug logs were making it to the sinks. – Sinaesthetic Jul 20 '17 at 20:26

1 Answers1

6

I know this is a pretty old question but I spent a couple of days dealing with a similar issue, so I will just leave some suggestions here.

  1. IF you are using the global Log object, make sure you are actually sending stuff. Serilog queues requests and sends them all once you execute:

    Log.CloseAndFlush();

    Without this, nothing will be logged. Make sure to execute this at the very end as after this, the logger will be closed (Serilog's Lifecycle documentation).

    If you're not using the global object, this should be executed once your logging object is disposed.

  2. Try to see if Serilog is actually showing errors by forcing one. Just delete one character in your connection string or something like that to see if Serilog's exception appears in the console.

JDC
  • 3,078
  • 1
  • 17
  • 15