-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientIpTelemetryInitializer.cs
44 lines (41 loc) · 1.67 KB
/
ClientIpTelemetryInitializer.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
namespace WcfApplicationInsights
{
using System;
using System.ServiceModel.Channels;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using WcfApplicationInsights.Implementation;
/// <summary>
/// Collects the IP of the client calling the WCF service.
/// </summary>
public sealed class ClientIpTelemetryInitializer : WcfTelemetryInitializer
{
/// <summary>
/// Initialize the telemetry event with the client IP if available.
/// </summary>
/// <param name="telemetry">The telemetry event.</param>
/// <param name="operation">The WCF operation context.</param>
protected override void OnInitialize(ITelemetry telemetry, IOperationContext operation)
{
if (string.IsNullOrEmpty(telemetry.Context.Location.Ip))
{
var location = operation.Request.Context.Location;
if (string.IsNullOrEmpty(location.Ip))
{
this.UpdateClientIp(location, operation);
}
telemetry.Context.Location.Ip = location.Ip;
}
}
private void UpdateClientIp(LocationContext location, IOperationContext operation)
{
if (operation.HasIncomingMessageProperty(RemoteEndpointMessageProperty.Name))
{
var property = (RemoteEndpointMessageProperty)
operation.GetIncomingMessageProperty(RemoteEndpointMessageProperty.Name);
location.Ip = property.Address;
WcfEventSource.Log.LocationIdSet(location.Ip);
}
}
}
}