Dominique
Dominique

Reputation: 17565

Why does my `NLog` logging command not log anything (despite NLog.config)?

I'm using NLog library for logging purposes.

I have declared the following class:

public partial class FenetrePrincipale : Form
{
    #region NLog
    private static readonly Logger logger = LogManager.GetLogger("FenetrePrincipale");
    #endregion
...

I run over the following lines of source code:

private bool StartService()
{
    try
    {
        logger.Debug("Try to start service");

My NLog.config looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">

  <targets>
    <target name="file"
            xsi:type="File"
            fileName="${basedir}/${date:format=yyyyMMdd}.log"
            layout="${longdate} | ${level:uppercase=true} | ${message}" />
  </targets>

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

</nlog>

... but no logfile gets created, although I'm sure i'm in the right directory (check the PID in Visual Studio, look for that in Windows Task Manager, and choose "Open file location" in the context menu).

Is there a way to alter logger.Debug(text); into logger.Debug(text,explanation) where the explanation is filled in by NLog and tells me why no logfile is created? (I don't believe this exists: I have decompiled NLog and I didn't find Debug(..., out ...))
Is there another way to find out what's going wrong?

For your information: it has worked before in that directory, it's certainly not a problem with file permissions or so.

Edit:
As proposed by Fildor, I have modified my nlog.config file as follows:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      internalLogFile="c:\Temp_Folder\log.txt" internalLogLevel="Trace">

... but no result:

Command prompt>dir c:\Temp_Folder\log.txt
...
 Directory of c:\Temp_Folder

File Not Found

However, my NLog.dll seems recent enough:

NLog details

... and the mentioned GitHub URL mentions quite regularly "introduced with NLog 4.3".

As far as NLog in my solution is concerned, this is what the solution looks like:

Solutions explorer screenshot

Is there a link with this eventlog?
In the Windows event log, "Windows Logs.System", there is following entry:

The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID {2593F8B9-4EAF-457C-B68A-50F6B8EA6B54} and APPID {15C20B67-12E7-4BB6-92BB-7AFF07997402} to the user DOMAIN\DDM SID (S-1-5-21-1049629045-689772303-932725714-51094) from address LocalHost (Using LRPC) running in the application container Unavailable SID (Unavailable). This security permission can be modified using the Component Services administrative tool.

Edit about adding 'NLog.config' to the Visual Studio solution:
Although I didn't believe in the sense of adding NLog.config to the Visual Solution file, I did it anyway: Visual Studio solution screenshot

This, however, did not make any difference: the logfile still does not get created, neither did the internal logfile (C:\Temp_Folder\Log.txt).

Upvotes: 0

Views: 73

Answers (3)

Dominique
Dominique

Reputation: 17565

What is BY FAR the easiest reason for a NLog.config file not to be taken into account?

Simple:

NLog is already configured elsewhere!

In my case: there are quite some Application_Name.config files, containing the <nlog tag.

... but why didn't I find the logfiles, created by those <nlog tags?

Don't laugh: this is what it looks like:

<nlog ...
  <variable name="logDirectory" value="\\fileserver\...

=> If by default, logs are written to a server, outside of your own computer, obviously you won't find anything while searching your PC 😀

Upvotes: 1

user1326493
user1326493

Reputation: 41

Took your 'NLog.config', and it works pretty fine.

  • Ensure the 'NLog.config' is marked as 'Copy if newer' in the Visual studio file Properties. The config file has to be copied in the output directory.

The initial NLog.config will produce a file like '20250227.log'

Upvotes: 2

Gael Piveteau
Gael Piveteau

Reputation: 31

On line 4 your schema location feels wrong, you might try to remove the ending NLog.xsd. also you can try to add throwExceptions="true". It helps find conf issues.

Something like that :

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true"
  throwExceptions="true"
  internalLogLevel="Info" internalLogFile="yourFilePath">

Upvotes: 2

Related Questions