I need to send Serilog logdata from Server A to Server B through a RabbitMQ channel. To do this I'm creating a serilog custom sink on Server A that sends the data. The problem is how It should send this data so Server B can easily re-log it with Serilog to an external log service and keep the structure logging intact.
The log code on Server A might look something like this :
var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 34;
log.Information("Processed {@Position} in {Elapsed:000} ms.", position, elapsedMs);
This will then be packed in to a LogEvent object by Serilog and then sent to the sinks :
I simple sink might look like this :
public class MySink : ILogEventSink
{
private readonly IFormatProvider _formatProvider;
public MySink(IFormatProvider formatProvider)
{
_formatProvider = formatProvider;
}
public void Emit(LogEvent logEvent)
{
//Sending logEvent over MQ
}
}
How can I send this LogEvent to Server b where I just can re-log it right in to Serilog and still preserve the structure logging without any lost data?
After some research I found the Serilog.Formatting.Compact NuGet that will translate the Serilog LogEvent to a compact JSON string. This JSON string can then be read back into a Serilog LogEvent with Serilog-formatting-compact-reader NuGET. After some testing I can conclude that this works great.
There might be types that the JSON sterilizer can´t handle but I haven’t found any yet.