-1

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).

8
  • As far as I remeber, there is a switch for "internal logging" that can be used to debug issues with NLog itself. (Or the config). If I remeber correctly that goes to a separate file, though. Commented Feb 27 at 14:30
  • See stackoverflow.com/a/60902387/982149 and/or github.com/NLog/NLog/wiki/Internal-logging Commented Feb 27 at 14:35
  • @Fildor: Thanks a lot for your quick reply, but it seems not to work, as you can see in the edit of my question. Commented Feb 27 at 14:51
  • How can I be sure that NLog is taking my nlog.config file into account? As mentioned, it's located in the same directory as my application. Commented Feb 27 at 15:09
  • 1
    @Dominique Pretty sure the EventLog-entry is not related to not being able to load/find the NLog.config-file. Again I suggest enabling the NLog InternalLogger from code to see why it cannot load the NLog.config-file. Commented Feb 28 at 17:54

3 Answers 3

3

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'

Sign up to request clarification or add additional context in comments.

4 Comments

NLog.config does not even appear in Visual Studio: there is only the reference to the main library, but that has worked before. I'll show what it looks like in question I'm about to edit.
In top of this, I don't have such a result anywhere: C:\>dir /S /B "20250227.*" results in File Not Found.
Although it might be possible to enforce configuring NLog from within the source code, I prefer solving this issue without modifying the code too much (NLog is being used over all our projects and I prefer not to need modifying the code in case something goes wrong).
2

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

1 Comment

I've added the "autoreload" and the "throwExceptions" but no results yet. Thanks for the tip, though.
1

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 😀

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.