Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary Cheetham 20 posts 144 karma points
    Apr 14, 2017 @ 12:33
    Gary Cheetham
    0

    How to get an ILogger in OnApplicationStarted (IApplicationEventHandler)?

    I'm trying to instantiate a DatabaseSchemaHelper in my OnApplicationStarted method. I have access to an UmbracoApplicationBase and an ApplicationContext.

    public class Startup : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // ....
        }
    
        // ....
    

    A required parameter in the constructor is an ILogger. I would also like to register an ILogger with my DI container.

    I can't seem to find a public ILogger in docs or intellisense. UmbracoApplicationBase has one but it's marked as protected.

    What is the right way to get an instance of ILogger here?

  • Jamie Pollock 174 posts 853 karma points c-trib
    Apr 16, 2017 @ 12:36
    Jamie Pollock
    101

    Hey Gary,
    This is the recommended way of creating a DatabaseSchemaHelper.

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        //Get the Umbraco Database context
        var dbContext = applicationContext.DatabaseContext;
        var logger = applicationContext.ProfilingLogger.Logger;
    
        var dbSchemaHelper = new DatabaseSchemaHelper(dbContext.Database, logger, dbContext.SqlSyntax);
    
        //Do awesome
    }
    

    Thanks,
    Jamie

Please Sign in or register to post replies

Write your reply to:

Draft