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?
Hi @grokky1 - this should be pretty straightforward, you've got two options:
ILogEventSink
and WriteTo.Sink(new YourEFSink(...))
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
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 overridingEmitBatchAsync()
is probably the way to go.HTH