You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
List of NuGet packages and version that you are using: Microsoft.ApplicationInsights (2.20.0)
Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the *.csproj file): v4.8
Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.): Windows
What are you trying to achieve?
I'm trying to create a custom telemetry initializer (HttpContextRequestTelemetryInitializer) like the one mentioned in the official Microsoft docs and then use it in a .NET framework 4.8 app. The code for the telemetry initializer looks something like this -
public class HttpContextRequestTelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor httpContextAccessor;
public HttpContextRequestTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor =
httpContextAccessor ??
throw new ArgumentNullException(nameof(httpContextAccessor));
}
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
var claims = this.httpContextAccessor.HttpContext.User.Claims;
Claim oidClaim = claims.FirstOrDefault(claim => claim.Type == "oid");
requestTelemetry.Properties.Add("UserOid", oidClaim?.Value);
}
}
What have you tried so far?
I'm able to use HttpContextRequestTelemetryInitializer as a singleton in .NET core application. But I'm not sure how to make it work in a .NET Framework 4.8 app, given that there are complexities of adding dependency injection and lack of httpContextAccessor in .NET Framework 4.8.
The text was updated successfully, but these errors were encountered:
I am not sure where you got this example from - I don't see that in any official docs. If it is in any official doc - please share here (so as to remove it from the doc)
It won't work for ASP.NET.
It may work for ASP.NET Core, but definitely not safe.
You may look at the example initializers in this repo and borrow that approach for ASP.NET and ASP.NET Core.
@sushanti Please use the approach from the existing initializers in this repo for aspnet and aspnetcore.
I have opened a doc issue to address the correctness issue.
List of NuGet packages and version that you are using: Microsoft.ApplicationInsights (2.20.0)
Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the
*.csproj
file): v4.8Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.): Windows
What are you trying to achieve?
I'm trying to create a custom telemetry initializer (HttpContextRequestTelemetryInitializer) like the one mentioned in the official Microsoft docs and then use it in a .NET framework 4.8 app. The code for the telemetry initializer looks something like this -
What have you tried so far?
I'm able to use HttpContextRequestTelemetryInitializer as a singleton in .NET core application. But I'm not sure how to make it work in a .NET Framework 4.8 app, given that there are complexities of adding dependency injection and lack of httpContextAccessor in .NET Framework 4.8.
The text was updated successfully, but these errors were encountered: