Undefined error in Logging Application Block

Sep 10, 2009 at 8:47 PM

Hi everyone,

My problem is about logging application block. I want to log items in a batch process. I added an entry to app.config for identifying the log entry if it is async or not. (<add key="AsyncLogEnabled" value="True" />). It takes to much time to write log especially in db that's why i want to handle this staff in another thread. Code runs without any problem when AsyncLogEnabled = False but when i change the app.config to AsyncLogEnabled = True, it fails without any exception..

In ProcessLog method's first line code ends running without any exception..

Thanks for your help..


  isAsyncLog = (ConfigurationManager.AppSettings["AsyncLogEnabled"] == "True");
  queuedLogEntry = new Queue<LogEntry>();
.
.
.
  if (Logger.ShouldLog(logEntry))
  {
      if (!isAsyncLog)
      {
          Logger.Write(logEntry); //RUNS OK
      }
      else
      {
          lock (queuedLogEntry)
          {
              queuedLogEntry.Enqueue(logEntry);
              if (!isAsyncActive)
              {
                  //CREATE ANOTHER THREAD
                  EventHandler handler = new EventHandler(WriteQueue);
                  handler.BeginInvoke(null, EventArgs.Empty, null, null);
              }
          }
      }
  }
.
.
.
        private static void WriteQueue(object sender, EventArgs e)
        {
            if (isAsyncActive)
                return;

            isAsyncActive = true;
            try
            {
                while (HasQueueEntry())
                {
                    try
                    {
                        LogEntry log;
                        lock (queuedLogEntry)
                        {
                            log = queuedLogEntry.Dequeue();
                        }

                        ProcessLog(log);
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                isAsyncActive = false;
            }
        }


        private static void ProcessLog(LogEntry log)
        {
            try
            {
                new ContextItems().ProcessContextItems(log); //ERROR HAPPENS IN THIS LINE..
                LogSource[] sArray = Logger.Writer.GetMatchingTraceSources(log).ToArray();

                TraceListenerFilter traceListenerFilter = new TraceListenerFilter();

                foreach (LogSource source in sArray)
                {
                    try
                    {
                        source.TraceData(log.Severity, log.EventId, log);//, traceListenerFilter);
                        continue;
                    }
                    catch (System.Exception)
                    {
                        Console.WriteLine("");
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

  catch (System.Exception e)
  {
      Console.WriteLine(e.Message);
  }

 

Sep 11, 2009 at 4:00 AM

Hi,

I tried your posted code but I didn't repro your scenario. Can you post your complete code? Or If possible, can you send us a quick repro project. Also, you might want to have a look at the distributor service of the ent lib http://msdn.microsoft.com/en-us/library/cc309264.aspx , see if it can achieve your goal.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 11, 2009 at 9:30 AM

Thank you for your quick reply.

I've solved -in fact realize- where i am wrong:) I am testing my application from a console application. And new thread has been started from that console application. That means new thread is depended to first thread. And when console application ends, depended thread ends too and is not able to finish the process. That's why there is no error and suddenly closes the new thread. When i write Console.Readline(); and wait for a while, i realize that log has been written.

Most important issue; if you open a new thread from a code, first application must keep on working for giving second thread chance to finish it's process:)

Thanks again..