Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I inject IHttpContextAccessor into custom TelemetryInitializer , from a 4.8 framework web application? #2551

Closed
sushanti opened this issue Mar 9, 2022 · 5 comments
Labels

Comments

@sushanti
Copy link

sushanti commented Mar 9, 2022

  • 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.

@cijothomas
Copy link
Contributor

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.

@cijothomas
Copy link
Contributor

Thanks @pharring for sharing. I'll open a doc issue to remove it or fix it.

Old discussions: #1313

@cijothomas
Copy link
Contributor

@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.

@sushanti
Copy link
Author

sushanti commented Mar 9, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants