serilog-settings-appsettings icon indicating copy to clipboard operation
serilog-settings-appsettings copied to clipboard

Allow configuration of multiple seq servers

Open schnitty opened this issue 5 years ago • 10 comments

If I want to write to two Seq servers I can do this in code by:

.WriteTo.Seq("http://localhost:5341")
.WriteTo.Seq("http://otherhost:5341")
.CreateLogger();

I'm not sure how I can do this in appSettings If I configure like so:

<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
<add key="serilog:write-to:Seq.serverUrl" value="http://otherhost:5341"/>

It ends up only sending logs to otherhost as the key is the same so it overwrites the first one. Is there away to make these settings unique so that it ends up sending logs to both servers?

schnitty avatar Apr 20 '20 08:04 schnitty

Hi! It's been a little while since I used this syntax, but this should be what you need:

<add key="serilog:using:FirstSeq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:FirstSeq.serverUrl" value="http://localhost:5341" />
<add key="serilog:using:OtherSeq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:OtherSeq.serverUrl" value="http://otherhost:5341"/>

nblumhardt avatar Apr 20 '20 21:04 nblumhardt

Thanks, that looks like it should work but it doesnt :(

schnitty avatar Apr 21 '20 00:04 schnitty

Sorry, that's me misremembering the features of Serilog.Settings.AppSettings. I think this is a limitation of the syntax. It's possible to use prefixes to work around it, i.e.:

<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
<add key="serilog-2:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog-2:write-to:Seq.serverUrl" value="http://otherhost:5341"/>

but this requires an additional ReadFrom.AppSettings("serilog-2") in the application.

I'll give this a bit more thought, it'd be nice to support this cleanly.

nblumhardt avatar Apr 21 '20 07:04 nblumhardt

Thanks for your help, I appreciate it. Unfortunately I can't get the workaround you listed to work either. It only picks up the first Sink, doesn't add the second "serilog-2" even when I add ReadFrom.AppSettings("serilog-2") in code

The intent is to have the server addresses configurable so my fallback is to create my own appsettings that contain the server addresses and then just reference them from code.

I'd prefer the all config option but if not workable my fallback plan will at least achieve the outcome of a web administrator being able to change the servers in config without having to rebuild the code.

schnitty avatar Apr 21 '20 08:04 schnitty

I don't suppose there's any chance your test harness is missing a Log.CloseAndFlush() call? That often presents as missing events from one or more loggers. (If you're not using the static Log class, then this corresponds with disposing the root Logger.)

nblumhardt avatar Apr 21 '20 23:04 nblumhardt

Good for raising it, that has certainly tripped me up in the past but I don't think that is the issue. When I create the logger and inspect its properties in the debugger it has only one Seq sink in its collection instead of two. So it doesn't seem to be picking up the separate non standard configuration.

web.config

...
    <add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
    <add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
    <add key="serilog2:using:Seq" value="Serilog.Sinks.Seq" />
    <add key="serilog2:write-to:Seq.serverUrl" value="http://otherhost:5341"/>
...

In debugger, I would expect two rows for Seq sinks, I only see one Debugger

schnitty avatar Apr 22 '20 00:04 schnitty

I'm also using Autofac for DI and have registered the logger according to https://github.com/nblumhardt/autofac-serilog-integration

schnitty avatar Apr 22 '20 00:04 schnitty

Sorry, my mistake again - just checked the role of settingPrefix - it's prepended in addition to serilog:, so the keys should be:

    <add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
    <add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
    <add key="serilog2:serilog:using:Seq" value="Serilog.Sinks.Seq" />
    <add key="serilog2:serilog:write-to:Seq.serverUrl" value="http://otherhost:5341"/>

nblumhardt avatar Apr 23 '20 00:04 nblumhardt

Awesome, that works. Thanks so much. Out of curiority where is the documenation that specified that? I haven't been able to find any.

schnitty avatar Apr 23 '20 00:04 schnitty

Great :+1: - it's mentioned in the intellisense docs for the parameter, I found it in: https://github.com/serilog/serilog-settings-appsettings/blob/dev/src/Serilog.Settings.AppSettings/AppSettingsLoggerConfigurationExtensions.cs#L62

Cheers, Nick

nblumhardt avatar Apr 23 '20 01:04 nblumhardt