Serilog: Sink for Entity Framework?

Created on 11 Mar 2017  路  3Comments  路  Source: serilog/serilog

New user here. I'm using ASP.NET Core and EF Core.

I know there is a sink to write to a SQL database. But is there a sink that writes to an EF context?

I'd like to convert a log event to an entity which is written to a database using EF. I also know this is "slow", and not best practice. But for our medium sized app, where all the db work is already handled by EF, it's more sensible to rely on EF than to start writing directly to the tables. And it'll be faster to implement.

If such a sink doesn't exist, I'd like to write one myself. But there's no info in the wiki about custom sinks. Where can I find such info?

question

Most helpful comment

This will register the custom sink: WriteTo.Sink(new YourEFSink(...)).

There's no way for a caller to await an async write to the logging pipeline, so in this case deriving from PeriodicBatchingSink and overriding EmitBatchAsync() is probably the way to go.

HTH

All 3 comments

Hi @grokky1 - this should be pretty straightforward, you've got two options:

  1. Just implement ILogEventSink and WriteTo.Sink(new YourEFSink(...))
  2. Make YourEFSink derive from PeriodicBatchingSink in _Serilog.Sinks.PeriodicBatching_ and override EmitBatchAsync()

Both techniques should work fine; the second will provide batching which can help with performance.

Cheers,
Nick

Thanks for the pointers. Also:

Do I need to somehow register that custom sink with Serilog?

My sink's work will be async, so is there a way to specify that?

This will register the custom sink: WriteTo.Sink(new YourEFSink(...)).

There's no way for a caller to await an async write to the logging pipeline, so in this case deriving from PeriodicBatchingSink and overriding EmitBatchAsync() is probably the way to go.

HTH

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ltd65 picture ltd65  路  3Comments

CADbloke picture CADbloke  路  4Comments

julealgon picture julealgon  路  4Comments

sirkirby picture sirkirby  路  3Comments

fernandezjose picture fernandezjose  路  4Comments