feat: Configure Sink via Serilog.Settings.Configuration
julealgon opened this issue · 1 comments
I just had another requirement where Map
made a lot of sense, however this project is using appsettings to configure the logger.
Since I didn't want to have parts of the logger configured in code and other parts configured in appsettings, I tried a few different json formats to make the configuration but I assume the parser doesn't currently understand the Action<T1, T2>
used on the default Map
overload.
I was able to work around it slightly using a custom extension method that performed some of the logic but it ended up with a lot of code duplication:
public static LoggerConfiguration MapToFile(
this LoggerSinkConfiguration loggerSinkConfiguration,
string keyPropertyName,
string pathFormat,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
long? fileSizeLimitBytes)
{
return loggerSinkConfiguration.Map(
keyPropertyName,
(key, config) => config.File(
string.Format(pathFormat, key),
rollOnFileSizeLimit: rollOnFileSizeLimit,
retainedFileCountLimit: retainedFileCountLimit,
fileSizeLimitBytes: fileSizeLimitBytes));
}
And then using this on appsettings:
"WriteTo": [
{
"Name": "MapToFile",
"Args": {
"KeyPropertyName": "Operation",
"FileSizeLimitBytes": 10485760,
"PathFormat": "/Logs/WebApps/MyApp/{0}.log",
"RetainedFileCountLimit": 100,
"RollOnFileSizeLimit": true
}
}
]
This is obviously less than ideal, as I had to duplicate a bunch of arguments used in File
.
Is it currently possible to configure the standard Map
using json (in which case I probably missed something), or can we add support for it?
Hi @julealgon, sorry about the slow reply, no, JSON/XML configuration support isn't really an option given the current design of the API of this sink. We might come up with some way to do this down the track, but it's unlikely at this stage. HTH!