private string GetWriteJsonString(IList <MetricEntity> logs) { List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); return(jsonstr); }
protected override void EndReport(string contextName) { base.EndReport(contextName); using (var client = new WebClient()) { var json = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); client.UploadString(this.influxdb, json); } this.data = null; }
/// <summary> /// 描述:异步将LogMetric写入Influxdb数据库 /// 作者:徐明祥 /// 日期:20150531 /// </summary> /// <param name="logs"></param> public void WriteAsync(IList <MetricEntity> logs) { if (logs == null || logs.Count == 0) { return; } List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } using (var client = new WebClient()) { var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); client.UploadStringAsync(this.influxdb, jsonstr); } }