static void Main(string[] args) { Console.WriteLine("Starting..."); //Logger configured with Autofac var builder = new ContainerBuilder(); builder.RegisterModule(new Nettolicious.Common.Logging.NLog.Autofac.Configuration()); builder.RegisterType <ValuesService>().As <IValuesService>(); var container = builder.Build(); var values = container.Resolve <IValuesService>(); values.Get(5); //No logger configured but getting IValuesService from Autofac builder = new ContainerBuilder(); builder.RegisterType <ValuesService>().As <IValuesService>(); container = builder.Build(); Console.WriteLine("Nothing should be logged after this"); values = container.Resolve <IValuesService>(); values.Get(); //No logger and no DI values = new ValuesService(); values.Get(); Console.WriteLine("Ending..."); Console.ReadLine(); }
public void GetShouldReturnString() { IValuesService service = new ValuesService(); var values = service.Get(1); values.ShouldBe(GetValue()); }
static void Main(string[] args) { Console.WriteLine("Starting..."); ILogger logger = new Logger(NLog.LogManager.GetCurrentClassLogger()); logger.Debug("Test message"); IValuesService values = new ValuesService(); values.Get(); values = new ValuesService(logger); values.Get(); var builder = new ContainerBuilder(); builder.Register(c => NLog.LogManager.GetCurrentClassLogger()).As <NLog.ILogger>(); builder.RegisterType <Logger>().As <ILogger>(); builder.RegisterType <ValuesService>().As <IValuesService>(); var container = builder.Build(); values = container.Resolve <IValuesService>(); values.Get(5); builder = new ContainerBuilder(); builder.RegisterType <ValuesService>().As <IValuesService>(); container = builder.Build(); Console.WriteLine("Nothing should be logged after this"); values = container.Resolve <IValuesService>(); values.Get(); Console.WriteLine("Ending..."); Console.ReadLine(); //TODO //https://stackoverflow.com/questions/28443955/how-to-inject-nlog-using-autofac-in-asp-net-webforms }