【问题标题】:DNN (DotNetNuke) module exceptions not loggingDNN (DotNetNuke) 模块异常未记录
【发布时间】:2016-12-18 18:11:03
【问题描述】:

我有一个客户的 DNN 站点,其自定义模块在模块错误日志记录方面存在问题。该站点已从 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");
        }
    }

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

【问题讨论】:

  • 升级是否改变了您的 DotNetNuke.log4net.config 中的任何内容?
  • @DotNetNuclear,log4Net 配置文件没有改变

标签: c# dotnetnuke


【解决方案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 可能会断开 Log4Net 进程与 EventLog 的连接。我不记得它在第 6 版中是如何工作的。

【讨论】:

  • 我测试了DotNetNuke.Services.Exceptions.Exceptions.LogException(ex),它可以工作。看来他们对 Log4Net 实现进行了重大更改,因为该方法并未被弃用。我会将您的答案标记为已接受,但我会在问题跟踪器中提交一张票。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多