asp.net mvc - Serilog MVC integration -
i have project utilizes 3rd party software uses serilog. purpose added following startup method in startup.cs file.
log.logger = new loggerconfiguration() .minimumlevel.verbose() .readfrom.configurationsection(configuration.getsection("logging:serilog")) .createlogger();
on separate project, have service layer uses logging mechanism based on microsoft.extensions.logging.ilogger.
now adding ui initial project , injecting microsoft.extensions.logging.ilogger controller classes through service collection; use (inside controllers) create service layer objects.
the challange is; inject existing serilog logger these controllers. couldn't find neat way of adding service collection controllers can fetch through di. suggestions?
the solution register instance of already-existing logger object in dependency injection container. single instance passed constructor of controller, instead of new instance being created each time.
the code depend on specific dependency injection framework using, in unity, be
container.registerinstance<ilogger>(log.logger)
this should go after code posted, in instantiate log.logger
.
you can additionally make code more reusable defining own ilogger
interface, described in this answer, controllers not tied serilog.ilogger
interface.
Comments
Post a Comment