Last active
June 15, 2020 23:37
-
-
Save dfar-io/699d6de582a6ba101bc6324a841faee8 to your computer and use it in GitHub Desktop.
A .NET Core Azure function that forwards EventHub events to a Loggly endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Microsoft.Azure.EventHubs" | |
using System; | |
using System.Text; | |
using Microsoft.Azure.EventHubs; | |
public static async Task Run(EventData[] events, ILogger log) | |
{ | |
var exceptions = new List<Exception>(); | |
foreach (EventData eventData in events) | |
{ | |
try | |
{ | |
string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count); | |
log.LogInformation($"message body: {messageBody}"); | |
// Replace these two lines with your processing logic. | |
var client = new HttpClient(); | |
var content = new StringContent(messageBody, Encoding.UTF8, "application/json"); | |
var response = await client.PostAsync("LOGGLY_ENDPOINT", content); | |
response.EnsureSuccessStatusCode(); | |
} | |
catch (Exception e) | |
{ | |
// We need to keep processing the rest of the batch - capture this exception and continue. | |
// Also, consider capturing details of the message that failed processing so it can be processed again later. | |
exceptions.Add(e); | |
} | |
} | |
// Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure. | |
if (exceptions.Count > 1) | |
throw new AggregateException(exceptions); | |
if (exceptions.Count == 1) | |
throw exceptions.Single(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment