static void OutputMessageInfo(string action, EventData data, MetricEvent info) { if (data == null) { return; } if (info != null) { Console.WriteLine("{0}{1} - Device {2}, Temperature {3}.", action, data, info.DeviceId, info.Temperature); } }
public void SendEvents() { // Create EventHubClient EventHubClient client = EventHubClient.Create(this.eventHubName); try { List <Task> tasks = new List <Task>(); // Send messages to Event Hub Console.WriteLine("Sending messages to Event Hub {0}", client.Path); Random random = new Random(); for (int i = 0; i < this.numberOfMessages; ++i) { // Create the device/temperature metric MetricEvent info = new MetricEvent() { DeviceId = random.Next(numberOfDevices), Temperature = random.Next(100) }; var serializedString = JsonConvert.SerializeObject(info); EventData data = new EventData(Encoding.UTF8.GetBytes(serializedString)) { PartitionKey = info.DeviceId.ToString() }; // Set user properties if needed data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString()); OutputMessageInfo("SENDING: ", data, info); // Send the metric to Event Hub tasks.Add(client.SendAsync(data)); } ; Task.WaitAll(tasks.ToArray()); } catch (Exception exp) { Console.WriteLine("Error on send: " + exp.Message); } client.CloseAsync().Wait(); }
public void SendEvents() { // Create EventHubClient EventHubClient client = EventHubClient.Create(this.eventHubName); try { List<Task> tasks = new List<Task>(); // Send messages to Event Hub Console.WriteLine("Sending messages to Event Hub {0}", client.Path); Random random = new Random(); for (int i = 0; i < this.numberOfMessages; ++i) { // Create the device/temperature metric MetricEvent info = new MetricEvent() {DeviceId = random.Next(numberOfDevices), Temperature = random.Next(100)}; var serializedString = JsonConvert.SerializeObject(info); EventData data = new EventData(Encoding.UTF8.GetBytes(serializedString)) { PartitionKey = info.DeviceId.ToString() }; // Set user properties if needed data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString()); OutputMessageInfo("SENDING: ", data, info); // Send the metric to Event Hub tasks.Add(client.SendAsync(data)); } ; Task.WaitAll(tasks.ToArray()); } catch (Exception exp) { Console.WriteLine("Error on send: " + exp.Message); } client.CloseAsync().Wait(); }