Search code examples
c#dotnetnuke

DNN (DotNetNuke) module exceptions not logging


I have a customer's DNN site with a custom module that's having issues with module error logging. The site was upgraded to version 7.4 from 6.2 and now to version 9.0. Module exceptions no longer appear in Admin / Host Events since the upgrade to 7.4. It appears module exception logging was changed in DNN 7.4 as explained here. This is the code that worked before but now nothing gets logged;

Test object:

public class foo
{
    public int id { get; set; }
    public string bar { get; set; }
}

Test webapi Controller:

[DnnAuthorize]
public class MyCustomModuleController : DnnApiController
{
    private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(MyCustomModuleController));


    [HttpGet]
    public HttpResponseMessage GetFoo(int id)
    {
        try
        {
            foo test = null;
            var bar = test.bar; //will throw null exception

            ...

        }
        catch (Exception ex)
        {
            Logger.Error(ex); //no log entries in db since ver. 7.4
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Server error");
        }
    }

Is there a setting that I should enable or is there a new way of logging events?


Solution

  • It is my understanding that DotNetNuke.Instrumentation.GetLogger() returns the logging object for doing the Log4Net logging to file appender logs stored in: /Portals/_default/Logs. You can also view them from Host > Host Settings > Logs.

    Normally, you would add to your class by setting it up in a constructor and calling the .Error()/.ErrorFormat(), .Warn()/.WarnFormat(), etc functions to append an error or information message to the logging file.

    public class MyCustomModuleController : DnnApiController
    {
        private DotNetNuke.Instrumentation.ILog Logger { get; set; }
    
        public MyCustomModuleController()
        {
            Logger = DotNetNuke.Instrumentation.LoggerSource.Instance.GetLogger(this.GetType());
        }
    
        public HttpResponseMessage GetFoo(int id)
        {
            try
            {
               ...
            }
            catch (Exception ex)
            {
                Logger.Error(ex); 
            }
        }
    }
    

    The other logging technique available is using the DotNetNuke.Services.Exceptions to log the exception to the database. These errors get added to the EventLog and Exceptions tables.

    public class MyCustomModuleController : DnnApiController
    {   
        public HttpResponseMessage GetFoo(int id)
        {
            try
            {
               ...
            }
            catch (Exception ex)
            {
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); 
            }
        }
    }
    

    It is possible that DNN disconnected the Log4Net process with the EventLog. I can't remember how it worked back in version 6.