ASP.NET 网站的 NLog 不记录

NLog for ASP.NET website doesn't log

我创建了简单的 ASP.NET 网站,添加了 NLog,但它没有创建任何日志文件,也没有抛出任何异常。我尝试了故障排除,但没有帮助 https://github.com/NLog/NLog/wiki/Logging-troubleshooting

我的web.config

<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog 
    xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    autoReload="true" 
    throwExceptions="true" 
    internalLogLevel="Off" 
    internalLogFile="c:\temp\nlog-internal.log">
    <targets>
        <target name="file" type="File" fileName="C:\log.txt" />
    </targets>
    <rules>
        <logger name="File" minlevel="Trace" writeTo="file" />
        <logger name="FileNotFound" minlevel="Trace" writeTo="file-404" />
    </rules>
</nlog>

然后我运行下面的页面,没有任何反应...

public partial class Test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LogManager.ThrowExceptions = true;
        Logger logger = LogManager.GetLogger("foo");

        Logger log = LogManager.GetCurrentClassLogger();

        log.Error("some test");
        logger.Error("some test 2");
    }
}

我有什么错?谢谢!

IIS 用户可能无法写入您的输出目录。尝试使用与网站相关的输出目录,例如 "logs".

<logger>name 属性是一个过滤器(按名称)

所以您现在过滤名称为 "File" 和 "FileNotFound"

的记录器

这会将所有内容写入文件

<logger name="*" minlevel="Trace" writeTo="file" />

另一种选择是在 C# 中使用命名记录器,而不是

Logger log = LogManager.GetCurrentClassLogger();

使用

Logger log = LogManager.GetLogger("FileNotFound");

然后您可以在配置中使用 <logger name="FileNotFound" ..>