首页 文章

DNN(DotNetNuke)模块异常不记录

提问于
浏览
0

我有一个客户's DNN site with a custom module that'有模块错误记录问题 . 该站点已从6.2升级到7.4版,现在升级到9.0版 . 自升级到7.4以来,管理/主机事件中不再出现模块异常 . 在DNN 7.4 as explained here中更改了模块异常日志记录 . 这是之前有效的代码,但现在没有记录任何内容;

测试对象:

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

测试webapi控制器:

[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");
        }
    }

是否有我应该启用的设置或是否有记录事件的新方法?

1 回答

  • 1

    据我所知,DotNetNuke.Instrumentation.GetLogger()返回日志对象,用于执行Log4Net日志记录到文件存储在/ / Portals / _default / Logs中的appender日志 . 您还可以从主机>主机设置>日志中查看它们 .

    通常,您可以通过在构造函数中设置并调用.Error()/ . ErrorFormat(),. Warn()/ . WarnFormat()等函数来添加到类中,以便向日志记录追加错误或信息消息文件 .

    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); 
            }
        }
    }
    

    另一种可用的日志记录技术是使用DotNetNuke.Services.Exceptions将异常记录到数据库中 . 这些错误会添加到EventLog和Exceptions表中 .

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

    DNN可能会使用EventLog断开Log4Net进程 . 我不记得它在第6版中是如何工作的 .

相关问题