NLog multiple instances

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

NLog multiple instances

Wizzard
Hello,
I am having trouble with logging from different classes into separate files. I have DLL, which uses NLog, and application, which uses also NLog, but also the DLL. So when the application starts, it logs events into the right file, but when the application starts the DLL, the logging events from the application are logged into the file specified in the DLL NLog settings. Weird, right?

This is how I log the events from the application:

        private static NLog.Logger logger;

        public static void InitLogger()
        {
            // set NLog
            logger = LogManager.GetLogger("DeviceManager");

            LoggingConfiguration config = new LoggingConfiguration();

            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            // Step 3. Set target properties
            fileTarget.FileName = "C:/ManagerLog/" + String.Format("{0:yyyy-MM-dd-hh-mm-ss}", DateTime.Now) + ".txt";
            fileTarget.Layout = "${longdate} | ${level:padding=7} | ${message}";

            // Step 4. Define rules
            LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule2);

            // Step 5. Activate the configuration
            LogManager.Configuration = config;
        }

        public static void Log(string message)
        {
            if (logger == null) InitLogger();

            logger.Trace(message);
        }





and this is how I init and log events in the DLL:





            // set NLog
            Logger logger = LogManager.GetLogger("Payout");

            LoggingConfiguration config = new LoggingConfiguration();

            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            // Step 3. Set target properties
            fileTarget.FileName = "C:/PayoutLog/" + String.Format("{0:yyyy-MM-dd-hh-mm-ss}", DateTime.Now) + ".txt";
            fileTarget.Layout = "${longdate} | ${level:padding=7} | ${stacktrace:topFrames=8:padding=250} | ${message}";

            // Step 4. Define rules
            LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule2);

            // Step 5. Activate the configuration
            LogManager.Configuration = config;




- the only difference is in getLogger(), fileName and layout.

So the question is, why NLog does not log into separate files - anyone knows whats wrong, and how can I solve this? Thanks for reply
pth
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: NLog multiple instances

pth
Hello,
I have exactly the same problem.
Any solution for this problem ?
Thanks for reply.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: NLog multiple instances

Jason LaFlair
Administrator
In reply to this post by Wizzard
NLog is doing exactly what you are telling it to do.  When your app runs and you log 'correctly' it uses that configuration.  When your app then calls the DLL and it runs its InitLogger - all you're really doing is re-configuring the core NLog configuration.

An application can not have multiple NLog configurations.
Loading...