Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up Serilog to call a stored procedure in SQL Server when logging messages?

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.

like image 265
user2023861 Avatar asked Nov 07 '16 15:11

user2023861


1 Answers

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();
like image 55
Nicholas Blumhardt Avatar answered Sep 20 '22 13:09

Nicholas Blumhardt