ihorko
ihorko

Reputation: 6945

NLog for ASP.NET website doesn't log

I created simple ASP.NET web-site, added NLog, but it doesn't create any log file, neither throws any exceptions. I tried troubleshoot, but it doesn't help https://github.com/NLog/NLog/wiki/Logging-troubleshooting

my 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>

Then I run page below, and nothing happens...

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

What is my fault? Thank you!

Upvotes: 1

Views: 1538

Answers (2)

Julian
Julian

Reputation: 36700

The name attriute of the <logger> is a filter (on name)

So you filter now on the loggers with the name "File" and "FileNotFound"

This will write all to the file

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

Another option is to use named loggers in C#, so instead of

Logger log = LogManager.GetCurrentClassLogger();

use

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

Then you could use <logger name="FileNotFound" ..> in your config

Upvotes: 1

djbyter
djbyter

Reputation: 793

Likely that your output directory can't be written to by the IIS user. Try using an output directory relative to the website, like "logs".

Upvotes: 0

Related Questions