Configure Serilog from database

564 views
Skip to first unread message

Pete Burgess

unread,
Jun 24, 2015, 6:44:28 AM6/24/15
to ser...@googlegroups.com
I'd like to be able to configure Serilog by getting values from a configuration database. I see that the configuration can be read from KeyValyePairs, but does this include which sinks are written to?

nblum...@nblumhardt.com

unread,
Jun 24, 2015, 4:41:57 PM6/24/15
to ser...@googlegroups.com, ger...@gmail.com
Hi Pete - yes, absolutely. So long as the DLL housing the Sink is loaded (or in the same folder as your application binaries) a key like "write-to:SinkName" will do it.

You can infer the syntax from this documentation: https://github.com/serilog/serilog/wiki/AppSettings#adding-a-sink (just omit the "serilog:").

The configuration system is extensible, so there are more direct ways to implement this, but reusing the key/value pair support will provide a bit of a head start.

Cheers!

Pete Burgess

unread,
Jun 25, 2015, 5:05:11 AM6/25/15
to nblum...@nblumhardt.com, ser...@googlegroups.com
Thanks Nicholas, I must be doing something wrong.

This works:
                ...
                loggerConfiguration.WriteTo.ColoredConsole();
                Serilog.Log.Logger = loggerConfiguration.CreateLogger();
                Serilog.Log.Logger.Information("it lives!");

this doesn't:
                ...
                loggerConfiguration.ReadFrom.KeyValuePairs(new[] { new KeyValuePair<string,string>("write-to:ColoredConsole", "") });
                Serilog.Log.Logger = loggerConfiguration.CreateLogger();
                Serilog.Log.Logger.Information("it dies!");

nor does splitting it into key-value:
                ...
                loggerConfiguration.ReadFrom.KeyValuePairs(new[] { new KeyValuePair<string,string>("write-to", "ColoredConsole") });
                Serilog.Log.Logger = loggerConfiguration.CreateLogger();
                Serilog.Log.Logger.Information("it dies!");

?

Further, will I also need the "using" key for extra sinks (e.g. Seq)?

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Serilog" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/serilog/FV9xBxCMKH0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to serilog+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nblum...@nblumhardt.com

unread,
Jun 26, 2015, 5:08:58 PM6/26/15
to ser...@googlegroups.com, ger...@gmail.com, nblum...@nblumhardt.com
Aaah - just realised it's probably this:


KeyValuePairs() is implemented in the core (portable) Serilog.dll and doesn't know about the sinks like ColoredConsole defined in Serilog.FullNetFx.dll - you'll need to add a line like the linked one to get this working.

(It's likely we'll eliminate this wart sometime in the transition to the VS2015 project/packaging tools.)

Cheers!
Nick
To unsubscribe from this group and all its topics, send an email to serilog+unsubscribe@googlegroups.com.

Pete Burgess

unread,
Jun 29, 2015, 3:36:48 PM6/29/15
to nblum...@nblumhardt.com, ser...@googlegroups.com
Thanks Nick, that's worked perfectly :D

LoggerConfiguration loggerConfiguration = new LoggerConfiguration(); loggerConfiguration.ReadFrom.KeyValuePairs(new[] { new KeyValuePair<string,string>("using:_CommandLineSettingsSerilogFullNetFx", "Serilog.FullNetFx"), new KeyValuePair<string,string>("write-to:ColoredConsole", "") }); Serilog.Log.Logger = loggerConfiguration.CreateLogger(); Serilog.Log.Logger.Information("it LIVES!");

PeteB

To unsubscribe from this group and all its topics, send an email to serilog+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Serilog" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/serilog/FV9xBxCMKH0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to serilog+u...@googlegroups.com.

Pete Burgess

unread,
Jun 30, 2015, 3:37:45 AM6/30/15
to nblum...@nblumhardt.com, ser...@googlegroups.com
Nick

Is there a list of setting key names for SeriLog and the Sinks?

I know you've said we can just "convert" the XML parameter by removing "serilog:" from the key name but not all of the sinks list all of the parameter names (e.g. RollingFile); do I have to look at the parameter names of the C# WriteTo extension methods (do these parameter names map exactly)?

PeteB

nblum...@nblumhardt.com

unread,
Jun 30, 2015, 6:34:26 AM6/30/15
to ser...@googlegroups.com, ger...@gmail.com
Hi Pete - no, though there are a few examples on the wiki, there's no comprehensive list out there right now. Going off of the C# method parameter names is the best option.

I'd love to improve the situation a bit, but our initial idea of adding this alongside the C# configuration sample for each sink turned out to create a lot of clutter and noise. Open to suggestions and ideas here.

Cheers!
To unsubscribe from this group and all its topics, send an email to serilog+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Serilog" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/serilog/FV9xBxCMKH0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to serilog+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages