1

I have a .net 4.6.2 web api project with NLog logging with only one target of type file.

The issue is that logging is behaving differently. Instead of appending logs to the same file continuously only first exception is getting logged and then nothing happens. I tried changing the layout of the generated file name so that I will get separate files on for each exception but still - files got generated only once, for only one exception (first exception after running the web api in debug mode or after mounting it in IIS).

This is how NLog sections looks like in my web.config

<configSections>  
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>  
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="logfile" xsi:type="File" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level}: ${message} ${exception:format=tostring}" fileName=".\Logs\test_${shortdate}.log" />
    </targets>
    <rules>
        <logger name="*" minlevel="Error" writeTo="logfile" />
    </rules>
</nlog>

Have you had such an experience ? How did you deal with it ?

  • 1
    Maybe enable [NLog InternalLogger](https://github.com/NLog/NLog/wiki/Internal-Logging) at `internalLogLevel="Info"` and look for clues. I'm guessing that either your application crashes / stalls, or that something unexpectedly changes the active NLog-configuration. See also https://github.com/NLog/NLog/wiki/Logging-troubleshooting – Rolf Kristensen Mar 14 '23 at 18:20
  • @RolfKristensen Do you know what is the correct place to shut down the logger in asp.net framework 4.6.2 web api ? – magicalKhachapuri Mar 16 '23 at 01:39
  • 1
    Believe each WebSite runs as an AppDomain in the IIS-process. NLog hooks into the AppDomain-Unload-event, so when website is restarted/closed then NLog will automatically flush / shutdown. So usually you don't have to perform explicit shutdown of NLog when running classic ASP.NET: – Rolf Kristensen Mar 16 '23 at 06:42
  • 1
    @RolfKristensen and that was the issue in my case, I was shutting down the logger in the action that I was calling :(, I removed it and now it works as expected. Thanks! – magicalKhachapuri Mar 16 '23 at 07:29

1 Answers1

1

I'm guessing that either your application crashes / stalls, or that something unexpectedly changes the active NLog-configuration. Try using the NLog InternalLogger with internalLogLevel="Info" to see what is happening. See also https://github.com/NLog/NLog/wiki/Logging-troubleshooting

Usually each ASP.NET WebSite is running as AppDomain in the IIS-process. NLog hooks into the AppDomain-Unload-event, so when website is restarted/closed then NLog will automatically flush/shutdown. So usually one doesn't have to perform explicit shutdown/flush of NLog when running classic ASP.NET.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70