/// <summary> /// Create a new value definition from the provided information. /// </summary> /// <param name="definition">The metric definition that owns this value definition</param> /// <param name="packet">The prepopulated value definition packet.</param> internal EventMetricValueDefinition(EventMetricDefinition definition, EventMetricValueDefinitionPacket packet) { m_Definition = definition; m_Packet = packet; //and determine if it is trendable or not m_Trendable = EventMetricDefinition.IsTrendableValueType(m_Packet.Type); MyIndex = -1; // Mark it as unknown. }
/// <summary> /// Records a value to the values array of this sample given its value definition. /// </summary> /// <remarks>The value must be defined as part of the event metric definition associated with this sample /// or an exception will be thrown. The data type must also be compatible with the data type configured /// on the event metric definition or no data will be recorded. /// If called more than once for the same value, the prior value will be replaced.</remarks> /// <param name="valueDefinition">The metric value definition object of the value to be recorded.</param> /// <param name="value">The value to be recorded.</param> public void SetValue(EventMetricValueDefinition valueDefinition, object value) { //make sure we got a value definition if (valueDefinition == null) { throw new ArgumentNullException(nameof(valueDefinition)); } //look up the numerical index in the collection so we know what offset to put it in the array at int valueIndex = Metric.Definition.Values.IndexOf(valueDefinition); //if we didn't find it, we're hosed if (valueIndex < 0) { #if DEBUG //if we're compiled in debug mode, tell the user they blew it. throw new ArgumentOutOfRangeException(nameof(valueDefinition), valueDefinition.Name); #else //log and return, nothing we can do. if (!Log.SilentMode) { Log.Write(LogMessageSeverity.Warning, LogCategory, "Unable to add metric value to the current sample due to missing value definition", "There is no value definition named {1} for metric definition {0}", Metric.Definition.Name, valueDefinition.Name); } return; #endif } //coerce it into the right type object storedValue; if (value == null) { //you can always store a null. And we can't really check it more, so there. storedValue = null; } else { //get the type so we can verify it Type valueType = value.GetType(); //is it close enough to what we're expecting? if (valueDefinition.IsTrendable) { if (EventMetricDefinition.IsTrendableValueType(valueType)) { storedValue = value; } else { //no, it should be trendable and it isn't. store null. storedValue = null; } } else { //we don't care what it is because we're going to coerce it to a string. storedValue = value.ToString(); } } //now write out the value to the correct spot in the array Packet.Values[valueIndex] = storedValue; }