File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed
articles/azure-monitor/app Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -485,13 +485,41 @@ The following sample initializer sets cloud role name to every tracked telemetry
485
485
``` csharp
486
486
public void Initialize (ITelemetry telemetry )
487
487
{
488
- if (string .IsNullOrEmpty (telemetry .Context .Cloud .RoleName ))
488
+ if (string .IsNullOrEmpty (telemetry .Context .Cloud .RoleName ))
489
489
{
490
490
telemetry .Context .Cloud .RoleName = " MyCloudRoleName" ;
491
491
}
492
492
}
493
493
```
494
494
495
+ #### Add information from HttpContext
496
+
497
+ The following sample initializer reads data from the [ ` HttpContext ` ] ( https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1 ) and appends it to a ` RequestTelemetry ` instance.
498
+
499
+ ``` csharp
500
+ public class HttpContextRequestTelemetryInitializer : ITelemetryInitializer
501
+ {
502
+ private readonly IHttpContextAccessor httpContextAccessor ;
503
+
504
+ public HttpContextRequestTelemetryInitializer (IHttpContextAccessor httpContextAccessor )
505
+ {
506
+ this .httpContextAccessor =
507
+ httpContextAccessor ??
508
+ throw new ArgumentNullException (nameof (httpContextAccessor ));
509
+ }
510
+
511
+ public void Initialize (ITelemetry telemetry )
512
+ {
513
+ var requestTelemetry = telemetry as RequestTelemetry ;
514
+ if (requestTelemetry == null ) return ;
515
+
516
+ var claims = this .httpContextAccessor .HttpContext .User .Claims ;
517
+ Claim oidClaim = claims .FirstOrDefault (claim => claim .Type == " oid" );
518
+ requestTelemetry .Properties .Add (" UserOid" , oidClaim ? .Value );
519
+ }
520
+ }
521
+ ```
522
+
495
523
## ITelemetryProcessor and ITelemetryInitializer
496
524
497
525
What's the difference between telemetry processors and telemetry initializers?
You can’t perform that action at this time.
0 commit comments