Closed cryo75 closed 3 years ago
There's a bit in the readme about JSON config here, and there's a sample using JSON config over in another repo here.
Basically, the answer is no... but yes :) There is nothing built-in for configuring hooks from appsettings. The recommended way is to point to a static instance that's already configured - but you can configure it yourself however you want (such as with appsettings). You could do that via a static ctor, some method that you called prior to configuring Serilog, or presumably you could also point it to a Lazy<MyHook>
too, e.g. (using HeaderWriter as an example, but principle is the same as this hook):
namespace Serilog.Sinks.File.Header.ConfigSample
{
public class SerilogHooks
{
private static myLazyHeaderWriter = new Lazy<HeaderWriter>(() => configure me here!);
public static HeaderWriter MyHeaderWriter => myLazyHeaderWriter.Value;
}
}
Ok I created an extension for IHostBuilder
and calling it before UseSerilog
. Is it possible to define turn on/off archiving? Does passing null
to the archive file ignore archiving?
No, as you can see here, passing null results in throwing, or the archived files remaining in the same folder as the original log files (depending on whether compression is enabled).
If you are configuring the hooks programmatically, you can just not pass the hooks into new LoggerConfiguration().WriteTo.File
.
Otherwise, if you're configuring hooks with JSON, it's a bit of an odd scenario - to want to use ArchiveHooks
... but maybe not use ArchiveHooks
😄, so there is nothing built-in for this scenario; ordinarily, if you don't want to use ArchiveHooks
, you'd just remove it from the JSON config. If you really want to enable the hooks via JSON and conditionally disable them programmatically with code, you could either fork this repo and add whatever you want, or you could do something like:
namespace Serilog.Sinks.File.Sample
{
public class SerilogHooks
{
private static myLazyHooks = new Lazy<FileLifecycleHooks>(() =>
{
// configure me here, returning *either* ArchiveHooks if you want to
// enable archiving, or NoopHooks if you don't
});
public static FileLifecycleHooks MyHooks => myLazyHooks.Value;
}
public class NoopHooks : FileLifecycleHooks
{
// Don't override anything - just use default behaviour
}
}
With your JSON config something like:
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"hooks": "Serilog.Sinks.File.Sample.SerilogHooks::MyHooks, Serilog.Sinks.File.SerilogHooks",
...
}
}
]
}
}
I would like to add additional settings (archive folder, wherether to compress, etc) in my appsettings file (in a separate section).
Is it possible to get these settings on Startup?