9

How do you set up Serilog to call a stored procedure in SQL Server when logging messages? I see how to use the MS SQL Server sink to store messages directly in a database table, but I'd like to call a stored procedure instead.

user2023861
  • 8,030
  • 9
  • 57
  • 86

1 Answers1

9

I'm not aware of any pre-built stored procedure sink for Serilog.

You can call the stored procedure yourself however by implementing the ILogEventSink interface:

class StoredProcedureSink : ILogEventSink
{
    public StoredProcedureSink(/* Connection info etc. */) { ... }

    public void Emit(LogEvent logEvent)
    {
        // Invoke sproc using logEvent data
    }
}

These are plugged in at configuration-time:

var sink = new StoredProcedureSink(/* ... */);

Log.Logger = new LoggerConfiguration()
    .WriteTo.Sink(sink)
    .CreateLogger();
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101