-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventHubsTriggerLogAnalytics.cs
72 lines (64 loc) · 3.35 KB
/
eventHubsTriggerLogAnalytics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Company.Function
{
public static class eventHubsTriggerLogAnalytics
{
[FunctionName("eventHubsTriggerLogAnalytics")]
public static async Task Run([EventHubTrigger("ehapimcustomlogspostapi", Connection = "EventHubConnectionAppSetting")] 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);
// Replace these two lines with your processing logic.
log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
await Task.Yield();
var logAnalyticsWorkspaceId = Environment.GetEnvironmentVariable("LogAnalyticsWorkspaceId");
var logAnalyticsWorkspaceKey = Environment.GetEnvironmentVariable("LogAnalyticsWorkspaceKey");
var logAnalyticsWorkspaceWorkspaceName = Environment.GetEnvironmentVariable("LogAnalyticsWorkspaceWorkspaceName");
AzureLogAnalytics azureLogAnalytics = new AzureLogAnalytics(
logAnalyticsWorkspaceId,
logAnalyticsWorkspaceKey,
logAnalyticsWorkspaceWorkspaceName,
"2016-04-01"
);
string[] words = messageBody.Split(',');
//DateTime.UtcNow, context.Deployment.ServiceName, context.RequestId, context.Request.IpAddress, context.Operation.Name, customCorrelationId
// 0, 1, 2, 3, 4, 5
var logMsg = new
{
DateTime = words[0],
ServiceName = words[1],
RequestId = words[2],
IpAddress = words[3],
OpName = words[4],
customCorrelationID = words[5].Substring(21)
};
var jsonLogMsg = JsonConvert.SerializeObject(logMsg);
azureLogAnalytics.Post(jsonLogMsg);
}
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();
}
}
}