I have a logger wrapper and I wanna inject serilog to it with following configurtion perse:
var logger = new LoggerConfiguration()
.WriteTo.RollingFile(
AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/Log-{Date}.txt")
.CreateLogger();
I cant seem to find a correct way to register it. I want it as singleton. Above is the instance.
I tried registering the instance above. Didnt work. I tried lambda, didnt work.
What works? anyone?
Maybe this helps:
builder.Register<ILogger>((c, p) =>
{
return new LoggerConfiguration()
.WriteTo.RollingFile(
AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/Log-{Date}.txt")
.CreateLogger();
}).SingleInstance();
I created a nuget-package with an extensions method for AutoFac's ContainerBuilder
.
All you need to do is install it and call the extension with either a logger-configuration or a log-path (and optional a template, you get a default).
var builder = new ContainerBuilder();
builder.RegisterSerilog("my-log-path"); // or a loggerconfiguration
var container = builder.Build();
var logger = container.Resolve<ILogger<MyClass>>();
logger.LogInformation("Hello World");
This also works perfectly with ASP.NET-Core. You find samples and the source-code on the github-repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With