Threading Issues - logs write fine when not in a thread...

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

Threading Issues - logs write fine when not in a thread...

andyg8180
If i run a stack of logs back to back with async on, it works just fine.
---------------------------------------------------------------------------------------------------------
            while (x < 1000)
            { new SolutionsSupport.Logs.Log().Trace("Hello from ProcessTaskA"); x+=1; }
---------------------------------------------------------------------------------------------------------

But the moment i try this same exact loop in a thread, i may get one log written before it crashes out with zero trace logs from nLog... It also throws an exception with no details, and gives me nothing to work with. Im trying to post to a DB, but the issue happens if im writing direct to a file as well, so im ruling out database connection issues... Am i "thread-safe" with nlog? This is how im doing the thread...
------------------------------------------------------------------------------------------------------------------------------------------
        public void TraceTest()
        {
            var threadA = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessTaskA));
            threadA.Start();
        }

        private void ProcessTaskA()
        {
            int x = 0;
            while (x < 1000)
            {
                new SolutionsSupport.Logs.Log().Trace("Hello from ProcessTaskA");
                x += 1;
            }
        }
------------------------------------------------------------------------------------------------------------------------------------------
here is the Log class i built class
------------------------------------------------------------------------------------------------------------------------------------------
    public class Log
    {
        private  static Logger logger;
        public Log() { logger = LogManager.GetCurrentClassLogger(); }
        public void Trace(string message) { logger.Trace(message); }
    }
------------------------------------------------------------------------------------------------------------------------------------------
This is my config
<?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"
      internalLogFile="D:\nlogtrace.txt"
      internalLogLevel="Debug"
      throwExceptions="true">


 
  <targets async="true">
    <target xsi:type="Database"
            name="databaselog"
            connectionString="Data Source=AGAGNE1;Initial Catalog=ClientSupport;Integrated Security=True"
            dbProvider="System.Data.SqlClient"
            commandText="INSERT INTO [Logs] (Stamp, ClientJobID, EventLevel, Message, StackTrace,MachineName) VALUES(@Stamp, 1, @EventLevel, @Message, @StackTrace, @MachineName)">

      <parameter name="@Stamp" layout="${date}"/>
      <parameter name="@EventLevel" layout="${level}"/>
      <parameter name="@Message" layout="${message}"/>
      <parameter name="@StackTrace" layout="${exception:stacktrace}"/>
      <parameter name="@MachineName" layout="${machinename}"/>
    </target>
    <target xsi:type="File"
            name="fileLog"
             fileName="D:\nlogTest.txt"
             />
  </targets>

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

any help would be appreciated...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threading Issues - logs write fine when not in a thread...

Jason LaFlair
Administrator
I just tried this copying your config and code and didn't seem to have an issue.  Even tried it with 8 different threads - all entries wrote to the file.

I also tried this with the version from Nuget as well as a locally compiled (more current) copy of NLog - still no issues.

Wish I could help further, but I just can't reproduce the issue :(

What type of an exception did it give you?  Are you running the v2 of Nlog?

Jason.
Loading...