Executing NLog asynchronously when calling code is also on separate thread

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Executing NLog asynchronously when calling code is also on separate thread

jchannon
This post was updated on .
I was wanting to check that NLog was running asynchronously. I set up a Async wrapper executed some code in a loop, paused the execution and looked in the Threads debug window and I could see a NLog thread.

I then put my loop on a separate thread and wanted to ensure that NLog was also on another thread but when looking in the Threads debug window I could not see anything.

Is there a way to determine that NLog is definitely running on a separate thread?

bk1
Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

bk1
I am interested in this as well.  Let me know what you find out.
Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

jchannon
I put the same question on stackoverflow.com but not heard anything either.

Had to implement this today so have made my own logger up unfortuantely but if I get an answer I'll come back to NLog.

Just surprised for such a popular library that there is very little help in the community.

Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

Jason LaFlair
Administrator
Maybe I'm not understanding this correctly but I just made a simple app that wrote logs from different threads and all worked just fine.

class Program
    {
        static void Main()
        {
            var actions = new List<Action>
                              {
                                  new Action(new ThreadLogger(100).TestThreadedLogging),
                                  new Action(new ThreadLogger(100).TestThreadedLogging),
                                  new Action(new ThreadLogger(500).TestThreadedLogging),
                                  new Action(new ThreadLogger(100).TestThreadedLogging)
                              };


            Parallel.Invoke(actions.ToArray());

            Console.ReadKey(true);
        }
    }

    public class ThreadLogger
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        public int Pause { get; set; }

        public ThreadLogger(int pause)
        {
            Pause = pause;
        }

        public void TestThreadedLogging()
        {
            for (int i = 0; i < 5; i++)
            {
                _logger.Debug("Testing " + i);

                Thread.Sleep(Pause);
            }
        }
    }

Here is my NLog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets async="true">
    <target xsi:type="Console" name="con" layout="ThreadID: ${threadid} - Message: ${message}"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="con" />
  </rules>
</nlog>
Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

jchannon
How do you know that the logger wrote on a separate thread? Putting the
thread id in the log shows the thread id from which the logger was called
from not the the thread id in which it was written on.


Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

Jason LaFlair
Administrator
So there's my misunderstanding, why does it matter?  It's logging the needed info from the thread making the call.
Reply | Threaded
Open this post in threaded view
|

Re: Executing NLog asynchronously when calling code is also on separate thread

jchannon
Its better performance if the logger is writing on another thread so the method can execute what it has to.