/// <summary> /// Adds an entry to the buffer. /// </summary> /// <param name="msg">The entry to be added.</param> public void AddMsgEntry(MsgEntry msg) { lock (_msgs.SyncRoot) { //if (!_last.Equals(msg)) { _msgs.Enqueue(msg); if (_msgs.Count >= _maxSize) { while (_msgs.Count >= _maxSize) { // TODO : add in the relevant API call to send the discard message to the default debug stream // OutputDebugString(....... _msgs.Dequeue(); } } } _last = msg; } }
/// <summary> /// Thread function which empties the queue when woken by the work event /// The QueueDrainedEvent is set when the Queue is emptied /// </summary> private void Worker() { for (;;) { if (_workEvent.WaitOne()) { if (_cxn == null) { Open(_connectionName); } if (_cxn != null) { SqlCommand cmd = new SqlCommand(_sqlProc, _cxn); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = CommandText; SqlParameter pCategory = cmd.Parameters.Add("@category", SqlDbType.NVarChar); SqlParameter pPriority = cmd.Parameters.Add("@priority", SqlDbType.Int); SqlParameter pMessage = cmd.Parameters.Add("@message", SqlDbType.NVarChar); SqlParameter pCallStack = cmd.Parameters.Add("@callStack", SqlDbType.NVarChar); SqlParameter pSource = cmd.Parameters.Add("@source", SqlDbType.NVarChar); SqlParameter pProcess = cmd.Parameters.Add("@process", SqlDbType.NVarChar); SqlParameter pThreadId = cmd.Parameters.Add("@threadId", SqlDbType.NVarChar); SqlParameter pUserId = cmd.Parameters.Add("@userId", SqlDbType.NVarChar); SqlParameter pMachineName = cmd.Parameters.Add("@machineName", SqlDbType.NVarChar); SqlParameter pLoggedTimeStamp = cmd.Parameters.Add("@loggedTimeStamp", SqlDbType.DateTime); MsgEntry msg = null; while ((msg = _msgs.Next()) != null) { pCategory.Value = msg._category; pPriority.Value = msg._priority; pMessage.Value = msg._message; pCallStack.Value = msg._callStack; pSource.Value = msg._source; string Process = string.Format("{0} {1}", System.Diagnostics.Process.GetCurrentProcess().ProcessName, System.Diagnostics.Process.GetCurrentProcess().Id); pProcess.Value = Process; pThreadId.Value = msg._threadId; pUserId.Value = msg._userId; pMachineName.Value = msg._machineName; pLoggedTimeStamp.Value = msg._loggedDate; try { cmd.ExecuteNonQuery(); } catch (Exception ex) { this.WriteLine( string.Format("AND::DbTraceListener - Worker method failed with following exception: {0}, for method {1} ", ex.ToString(), "Worker()"), "Error", "DbTraceListener" ); this.WriteEntryToInternalLog(string.Format("Open connection failed with following exception: {0}", ex.ToString())); } } // Queue is empty - ok to dispose _queueDrainEvent.Set(); } } } }