Unable to log using the ILogger Interface

Topics: Writing modules
Mar 23, 2012 at 6:51 PM

Hello, I am very new to Orchard! I am unable to get the logger to work...the log files in AppData\Logs are empty. Here is my Controller code:

    [Themed]
    public class HomeController : Controller
    {
        private readonly IOrchardServices _services;
        private readonly IBookingService _bookingService;
        private readonly ILogger _logger;

        public HomeController(IOrchardServices ser, IBookingService bookingSer)
        {
            _services = ser;
            _bookingService = bookingSer;
            _logger = NullLogger.Instance;
        }

        public ActionResult Index()
        {
            _logger.Information("I am in Home Controller Index");
            return View();
        }

        public ActionResult Availability(String HotelId = "11206", String CheckIn = "2012-06-01", String CheckOut = "2012-06-03", String Provider = "SynXis")
        {
            _logger.Information("I am in Home Controller Availability");
            return View(_bookingService.GetAvailability(HotelId, CheckIn, CheckOut));
        }
    }

Can you help me please?

Regards,

KH

Coordinator
Mar 23, 2012 at 7:16 PM

The logger must be a public proeprty with at least a get accessor.

Full example:

public ILogger Logger {get ;set; }

Mar 23, 2012 at 7:26 PM

Thank you for your quick reply. But I tried this as well. So my code looked like this....it did not make a difference:

    public class HomeController : Controller
    {
        private readonly IOrchardServices _services;
        private readonly IBookingService _bookingService;

        public ILogger Logger { get; set; }

        public HomeController(IOrchardServices ser, IBookingService bookingSer)
        {
            _services = ser;
            _bookingService = bookingSer;
            Logger = NullLogger.Instance;
        }

        public ActionResult Index()
        {
            Logger.Information("I am in Home Controller Index");
            return View();
        }

        public ActionResult Availability(String HotelId = "11206", String CheckIn = "2012-06-01", String CheckOut = "2012-06-03", String Provider = "SynXis")
        {
            Logger.Information("I am in Home Controller Availability");
            return View(_bookingService.GetAvailability(HotelId, CheckIn, CheckOut));
        }
    }

Coordinator
Mar 23, 2012 at 7:52 PM

This is because you are using .Information(). It's not rendered with the default switches. If you use .Debug or .Error you will see womething. Or go inside /config/log4net to change the levels.

Mar 24, 2012 at 5:21 AM

Thank you so much for replying. However, I already tried Debug and Error and both do not produce anything. I see that the .log file is being created but it is always empty.

Regards,

KH

Mar 26, 2012 at 9:48 PM

It's not flushed to the file yet. Stop the site and take a look again.