dependency injection - Passing in the type of the declaring class for NLog using Autofac -
dependency injection - Passing in the type of the declaring class for NLog using Autofac -
following on this question autofac inject type of declaring object constructor of nlog service, can correctly log type logging entries.
my nlogservice class looks this...
public class nlogservice : ilogservice { private readonly logger _logger; public nlogservice(type t) { var consumertype = t.declaringtype.fullname; _logger = logmanager.getlogger(consumertype); }
however fails on app startup because cannot work out inject constructor of nlogservice next error...
none of constructors found 'public binding flags' on type 'myproduct.domain.services.logging.nlogservice' can invoked available services , parameters: cannot resolve parameter 'system.type t' of constructor 'void .ctor(system.type)'.
so, question - how instruct autofac inject type of calling class?
i tried this...
public nlogservice(type t) { var method = methodbase.getcurrentmethod(); type consumingtype = method.declaringtype; var consumertype = consumingtype.fullname; var consumertype = t.declaringtype.fullname; _logger = logmanager.getlogger(consumertype); }
but end myproduct.domain.services.logging.nlogservice
what want type of class doing actual logging.
i have tried suggestion , didnt work me either.
could create nlogservice generic, i.e. nlogservice<t>
, utilize autofac's open generics support?
then this:
class="lang-csh prettyprint-override">public class nlogservice<t> : ilogger<t> { private readonly logger _logger; public nlogservice() { _logger = logmanager.getlogger(typeof(t).fullname); } }
dependency-injection autofac nlog
Comments
Post a Comment