Another alternative is to write a custom implementation of our ILogWriter
interface that passes all calls to Serilog and converts the log info as needed:
public class LogWriterAdapter : LogWriterBase
{
private readonly ILogger _logger;
private const string MessageTemplate = "{type} {id} {area} {message}";
private const string MessageTemplateWithData = "{type} {id} {area} {message} {data}";
public LogWriterAdapter(ILogger logger, LogLevel level)
{
Level = level;
_logger = logger;
}
private void Write(LogLevel level, Type objectType, int objectId, string area, string message, ArraySegment<byte>? data)
{
string template = (data == null) ? MessageTemplate : MessageTemplateWithData;
if (level <= LogLevel.Verbose)
{
_logger.Verbose(template, objectType, objectId, area, message, data);
}
else if (level <= LogLevel.Debug)
{
_logger.Debug(template, objectType, objectId, area, message, data);
}
else if (level <= LogLevel.Info)
{
_logger.Information(template, objectType, objectId, area, message, data);
}
else if (level <= LogLevel.Error)
{
_logger.Error(template, objectType, objectId, area, message, data);
}
}
public override void Write(LogLevel level, Type objectType, int objectId, string area, string message)
{
Write(level, objectType, objectId, area, message, null);
}
public override void Write(LogLevel level, Type objectType, int objectId, string area, string message, byte[] buffer, int offset, int length)
{
Write(level, objectType, objectId, area, message, new ArraySegment<byte>(buffer, offset, length));
}
}
Please note ILogWriter has no concept equivalent to Serilog’s structured event data, but it can at least provide some of the logged information in a slightly structured way.
To utilize the LogWriterAdapter class, you would do something like this:
// obtain an appropriate logger
ILogger logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
// create an instance of Rebex object
var client = new Sftp();
// use LogWriterAdapter (with an equivalent level specified)
client.LogWriter = new LogWriterAdapter(logger, LogLevel.Debug);
// do the rest
client.Connect(...)