serilog / serilog-sinks-email

A Serilog sink that writes events to SMTP email
Apache License 2.0
73 stars 68 forks source link

Open scope to retrieve created sink #43

Closed WiredSharp closed 6 years ago

WiredSharp commented 7 years ago

Hi, I created a failover sink which allow to switch to another sink when first fail. I need to get an instance of the sink to provide it to the failover sink constructor. Unfortunately, current architecture for email sink do not allow to manipulate sink instance as emailSink class is internal. It would be nice if construction and configuration injection are decoupled,

nblumhardt commented 7 years ago

Hi! I think LoggerSinkConfiguration.Wrap() should be able to solve this problem for you. There's not much info beyond the discussion in the PR, so let me know if you have trouble fitting it in (check out how Serilog.Sinks.Async uses it: https://github.com/serilog/serilog-sinks-async/blob/dev/src/Serilog.Sinks.Async/LoggerConfigurationAsyncExtensions.cs#L48).

Cheers!

WiredSharp commented 7 years ago

Hi and thanks for your reply. unfortunately, the problem is that i can't create an email sink as the only way is from the static extension class. The wrap helper is based on a function which returns a sink. My own FailoverSink is based on the same concept.

nblumhardt commented 7 years ago

Hi @zericco - if you substitute BackgroundWorkerSink with FailoverSink in the example, you should get what you need, though:

.WriteTo.Failover(wt => wt.Email(...), failover => failover.File(...))

The first arg is the configure passed through to Wrap, the second is used to create the failover target:

            var alternate = new LoggerConfiguration();
            failover(alternate.WriteTo);
            return LoggerSinkConfiguration.Wrap(
                loggerSinkConfiguration,
                wrappedSink => new FailoverSink(wrappedSink, alternate.CreateLogger()),
                configure);

What do you think?

WiredSharp commented 7 years ago

@nblumhardt : effectively, it's quite a clever solution. I did not catch that Serilog.Logger implements Serilog.ILogEventSink. Is there any safeguard against cycle if a logger becomes its own sink? Again, thanks for your help.

WiredSharp commented 7 years ago

@nblumhardt : effectively, it's quite a clever solution. I did not catch that Serilog.Logger implements Serilog.ILogEventSink. Is there any safeguard against cycle if a logger becomes its own sink? Again, thanks for your help.

nblumhardt commented 7 years ago

In a way, there's a safeguard - it would throw StackOverflowException as soon as an event was logged through it :-)

phoenyx2k commented 5 years ago

@zericco - Could I get the code for your FailoverSink? I am trying to do something similar.

WiredSharp commented 5 years ago

@zericco - Could I get the code for your FailoverSink? I am trying to do something similar. Hi, here it is, it was a basic try: https://gist.github.com/zericco/1417646a21c0541b7f03f755d0697fe1