/// <summary> /// Adds the tracker payload to the emitter. /// </summary> /// <param name="payload">The base event payload.</param> /// <param name="contexts">The list of contexts from the event.</param> private void AddTrackerPayload(TrackerPayload payload, List <IContext> contexts, string eventId) { // Add default parameters to the payload payload.Add(Constants.PLATFORM, this.platform.Value); payload.Add(Constants.APP_ID, this.appId); payload.Add(Constants.NAMESPACE, this.trackerNamespace); payload.Add(Constants.TRACKER_VERSION, Version.VERSION); // Add the subject data if available if (subject != null) { payload.AddDict(subject.GetPayload().GetDictionary()); } // Add the session context if available if (session != null) { contexts.Add(session.GetSessionContext(eventId)); } // Build the final context and add it to the payload if (contexts != null && contexts.Count > 0) { SelfDescribingJson envelope = GetFinalContext(contexts); payload.AddJson(envelope.GetDictionary(), this.base64Encoded, Constants.CONTEXT_ENCODED, Constants.CONTEXT); } Log.Verbose("Tracker: Sending event to the emitter."); Log.Verbose(" + Event: " + payload.ToString()); // Add the event to the emitter. emitter.Add(payload); }
// --- Database Functions /// <summary> /// Adds an event payload to the database. /// </summary> /// <returns><c>true</c>, if event was added, <c>false</c> otherwise.</returns> /// <param name="payload">An event payload</param> public bool AddEvent(TrackerPayload payload) { try { _dbLock.EnterWriteLock(); // Get event collection var col = _db.GetCollection <Event>(COLLECTION_NAME); col.Insert(new Event { Payload = payload.ToString(), Id = Guid.NewGuid(), CreatedAt = DateTime.UtcNow }); Log.Verbose("EventStore: Event added"); return(true); } catch (Exception e) { Log.Error("EventStore: Event add failed"); Log.Error(e.ToString()); return(false); } finally { _dbLock.ExitWriteLock(); } }
public void TestAddJsonFunction() { TrackerPayload payload = new TrackerPayload(); Dictionary <string, object> dict = new Dictionary <string, object> (); dict.Add("hello", "world"); Assert.AreEqual(0, payload.GetDictionary().Count); payload.AddJson(dict, false, "encoded", "not_encoded"); Assert.AreEqual(1, payload.GetDictionary().Count); Assert.AreEqual("{\"hello\":\"world\"}", payload.GetDictionary()["not_encoded"]); Assert.AreEqual(39, payload.GetByteSize()); Assert.AreEqual("{\"not_encoded\":\"{\\\"hello\\\":\\\"world\\\"}\"}", payload.ToString()); }