Search code examples
error-handlingdotnetnukednn9

DNN 9.8 - EventLogController is obsolete - Does anyone have an example on how to use the Dependency Injection method to fix this error?


Does anyone perhaps have an example to replace the old Error logging in DNN module?

I have looked at the following articles:

  1. https://dnncommunity.org/forums/aft/1527
  2. Has anyone implemented DotNetNuke.Abstractions.Portals.IPortalAliasInfo.HttpAlias in DNN version 9.9?

I currently get the following error: enter image description here

catch (Exception ex)
    {
      EventLogController logController = new EventLogController();
      logController.AddLog("Problem Getting Product Description, Title, or Image URL.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
    }

Do you need to create a startup file? If so, do you need to create a startup file for each module or put it in the root folder?


Solution

  • Here is some code that works for me:

    using DotNetNuke.Abstractions;
    using DotNetNuke.Abstractions.Logging;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace myCompany.DNN.Modules.myModule {
       private readonly IEventLogger _eventLogger;
       public class myControl {
          public myControl() {      // this is the constructor of the class
             _eventlogger = DependencyProvider.GetRequiredService<IEventLogger>();
          }
       }
    
       protected override void someEvent(object sender, EventArgs e) {
          try {
             // some code
          } catch(Exception ex) {
             _eventLogger.AddLog("Problem Getting Product Description, Title, or Image URL.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
          }
       }
    }
    

    And this article could be useful, too...