Nlog: [question] NLog.dll is loaded before very 1 call to it.

Created on 21 Jul 2020  路  4Comments  路  Source: NLog/NLog

Hi, searched all the web an didn't found the answer. Why NLog.dll is forced to load on app (WinForms/console) startup?
Before very 1 call to Nlog - on Main open { NLog.dll is already locked. Internal log (internalLogLevel="Trace") has no entries to this time. Is there a chance to have a normal .net assemble behavior - _loaded when first referenced in code_?
And second question - how do you achieve this? :)
Thanks.

question

Most helpful comment

When the program execution reaches a new method (like void Main()) then it compiles the entire method.

This means it will lookup the types referenced in the method, and checks if the invoked methods are available and then generates machine code.

If you don't reference NLog in your void Main() then I'm pretty sure it will not automatically load NLog-assembly.

All 4 comments

Pretty sure NLog-assembly is not doing anything special. But if you have this in your application program.cs then you will probably see the behavior:

```c#
using System;

namespace ConsoleApplication
{
public class Program
{
static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); // static variables are loaded early

    public static void Main(string[] args)
    {
        Logger.Info("Hello World");
    }
}

}
```

P.S. Good idea to fill out the issue template, when asking questions instead having to pull out the information from you. Ex. are you using .NetFramework or .NetCore ?

@snakefoot

P.S. Good idea to fill out the issue template, when asking questions instead having to pull out the information from you. Ex. are you using .NetFramework or .NetCore ?

Sorry for that :( Not following the rules is not in my manner, but I was just shocked by Nlog's behavior...
Reproducible with both net472 and netcoreapp3.1. Nlog 4.7.2. Windows 7 x64.

using System;

namespace NlogDllTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to load Nlog...");
            Console.ReadKey();
            NLog.LogManager.GetCurrentClassLogger().Info("NLog loaded!");
        }
    }
}

Snipaste_2020-07-21_20-09-51

Furthermore if I delete NLog.dll from Debug dir, and run debug - exception throws before managed code :thinking: :

Snipaste_2020-07-21_20-11-09

Sample project is here:
NlogDllTest.zip

When the program execution reaches a new method (like void Main()) then it compiles the entire method.

This means it will lookup the types referenced in the method, and checks if the invoked methods are available and then generates machine code.

If you don't reference NLog in your void Main() then I'm pretty sure it will not automatically load NLog-assembly.

Ah, my bad :((( It is never too late to learn...
Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sszost picture sszost  路  3Comments

smeegoan picture smeegoan  路  3Comments

MaximRouiller picture MaximRouiller  路  3Comments

ranjan-2209 picture ranjan-2209  路  3Comments

haythamabutair picture haythamabutair  路  3Comments