Howto log to 2 instances of same type of sink (Seq)?

507 views Asked by At

Possible? Cannot find a "sink forwarder", where one sink can forward to several other sinks, possibly of the same type.

Serilogs documentation (https://github.com/serilog/serilog/wiki/AppSettings) clearly states that

NOTE: When using serilog: keys need to be unique.*

so adding the same Seq sink several times doesnt seem to be a good idea.

I'm looking for the same concept as in log4net, where one logger can hold several appenders.

2

There are 2 answers

0
Nicholas Blumhardt On BEST ANSWER

Unfortunately the <appSettings> config provider for Serilog doesn't support this case; the appSettings.json one does, if you're able to use it, otherwise configuring the sinks in code WriteTo.Seq(...).WriteTo.Seq(...) is the way to go.

0
Toke Breer-Mortensen On

Semi-workaround style of solution:

  1. Put a "read these keys" in appsettigs

Example 1: Read one key

<add key="SerilogToHttpKeys" value="MyMachineA" />

Example 2 (which solves the problem): Read many keys

<add key="SerilogToHttpKeys" value="MyMachineA, MyLocalMachine, MachineOnTheMoon" />

Both cases "points" to an unlimited number of keys, that are then read via code (see 2) and hence be tweaked w/out recompiling

<add key="MyLocalMachine" value="http://localhost:5341/;juzOPqqqqqqqq" />
<add key="MyMachineA" value="http://10.107.14.57:5341/;m8QVnDaqqqqqqqqqqqqq" />
<add key="MachineOnTheMoon" value="http://10.107.14.62:5341/;Ah0tSzqqqqqqqqqqqq" 
  1. Loop the keys in code - each key points to a http address with an API key, which is used for logging to Seq, but change the structure of each entry, and you could log to file ect.

     foreach (var aKey in System.Configuration.ConfigurationManager.AppSettings.Get("SerilogToHttpKeys")
                                .Split(',')//Use , as separator 
                                .Select(s => s.Trim()))
     {
         var    fields    = System.Configuration.ConfigurationManager.AppSettings.Get(aKey);
         var    separator = ';';
         string serverUrl = fields.Split(separator)[0];
         string apiKey    = fields.Split(separator)[1];
    
         loggerConfiguration = loggerConfiguration.WriteTo.Seq(serverUrl: serverUrl, apiKey: apiKey);
     }
    

I use this for logging both to my server and my dev machine at the same time - its easier to keep the localhost Seq open when errors occour, and see if I can find them there instead of logging into the server. However, in case my devmachine is not online, I have the logs on the server as well. Off course, if more than one persons accesses the server licenses are needed for Seq, but in a simple "one dev, one dev-machine, one server" it works.