0

How to get the object of ITelemety object in an ASP.NET Core custom ActionFilterAttribute using dependency injection?

Platform - .Net Core

What is required : ITelemetry object

Sample Code for reference

  public class CustomActionFilterAttribute : ActionFilterAttribute
  {
    private readonly ITelemetry telemetry;

    public CustomActionFilterAttribute(ITelemetry telemetry)
    {
        this.telemetry= telemetry;
    }
   }

I need to resolve ITelemetry object to add few Global Properties in the request context so that those can be logged in Request log. With above sample code, an exception is thrown when ITelemetry object is not being resolved by DI.

The CustomActionFilterAttribute is injected as shown below in startup.

services.AddMvc(options =>
 {
       options.Filters.Add<CustomActionFilterAttribute>();
 });
user1672994
  • 10,509
  • 1
  • 19
  • 32
  • why not use ITelemetry initializer? it's more easier for your purpose. – Ivan Glasenberg Jul 23 '19 at 06:38
  • @IvanYang Thanks for your response. I require [ActionExecutingContext.ActionArguments](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filters.actionexecutingcontext.actionarguments?view=aspnetcore-2.2#Microsoft_AspNetCore_Mvc_Filters_ActionExecutingContext_ActionArguments) to log the request arguments. The request arguments can not be retrieved in `ITelemetryInitializer`. Since, the custom filter has access to `ActionExecutingContext.ActionArguments` so using `ITelemetry` object the arguments can be logged. – user1672994 Jul 23 '19 at 08:42
  • @IvanYang - Currently, I've used `ITelemetryInitializer` to store `ITelemetry` object in `HttpContext.Items` and later retrieving the object in the custom `ActionFilterAttribute`. The implementation is done as mentioned in this [link](https://stackoverflow.com/questions/42686363/view-post-request-body-in-application-insights/53776460#53776460) – user1672994 Jul 24 '19 at 06:16
  • You can put your comment as an answer, that would help others :). – Ivan Glasenberg Jul 24 '19 at 06:21
  • The way it is implemented is more like a workaround instead of appropriate solution. The solution I'm looking is --- to inject `ITelemetry` object in custom `ActionFilterAttribute`. Is it possible? – user1672994 Jul 24 '19 at 08:37
  • Currently, I cannot find a way to inject ITlemetry object, but if I can figure it out, will let you know. – Ivan Glasenberg Jul 25 '19 at 01:07
  • And .ctor injection of a `IHttpContextAccessor` (which will be null at times) into a custom `ITelemetryInitializer` does not meet your needs because `ActionExecutingContext.ActionArguments` gives you more structured info on your arguments then `HttpContext.Request` ? – ironstone13 Aug 07 '19 at 19:52
  • @ironstone13 - I did not get you what you are referring here. The mentioned linked solution is a workaround instead of actual **solution**. I'm also looking for a **solution** which has no dependency to inject `ITelemetry` object in `IHttpContextAccessor`. Is that possible? – user1672994 Aug 13 '19 at 04:27

0 Answers0