title: Application Insights with multiple applications
body: <p>I have an Application Insights which logs traces from an App Service and an App Function (one resource for 2 functions). 
  I need to filter traces according to the resource (App Service or App Function) and, if possible, for the App Function which function is actually logging.
  Looking at the traces I see the following list of properties:</p>
  
  <p><a href="https://i.stack.imgur.com/yQ8IQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yQ8IQ.png" alt="enter image description here"></a></p>
  
  <p>I thought to find the resource name in the <code>appName</code> property, instead there is the Application Insights resource name, which is useless for me, since all those traces are from that resource.</p>
  
  <p>Note: I don't like the workaround to set a prefix in the message to filter the traces.</p>
  
  <h1><strong>UPDATE</strong></h1>
  
  <p>I followed Peter Bons suggestions and I created a brand new Function V3 project. The basic version of the project worked also without the Telemetry Initializer, I mean that the Cloud_RoleName property was correctly populated. 
  Then, I added my changes to adapt the sample code and I found that the problem comes up when I inject a new Telemetry Client. I know, it is not recommended to manually inject TelemetryClient in App Function, but I absolutely need to send Custom Event to Application Insights and, as far as I know, it is not possible with ILogger interface used by default in App Function.</p>
  
  <p>Startup.cs</p>
  
  <pre><code>public class Startup : FunctionsStartup
  {
      private TelemetryConfiguration telemetryConfiguration;
  
      public override void Configure(IFunctionsHostBuilder builder)
      {
          var localRoot = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
          var azureRoot = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
  
          var configBuilder = new ConfigurationBuilder()
              .SetBasePath(localRoot ?? azureRoot)
              .AddEnvironmentVariables()
              .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
  
          var configuration = configBuilder.Build();
  
          if (builder != null)
          {
              this.ConfigureServices(builder.Services, configuration);
          }
      }
  
      private void ConfigureServices(IServiceCollection services, IConfiguration configuration)
      {
          services.AddSingleton&lt;ITelemetryInitializer&gt;(x =&gt; new CustomTelemetryInitializer(configuration["appFunctionName"]));
          telemetryConfiguration = new TelemetryConfiguration(configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);
          telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
          var telemetryClient = new TelemetryClient(telemetryConfiguration);
          services.AddSingleton(telemetryClient);
  
          services.AddSingleton&lt;ISampleInterface, SampleService&gt;();
      }
  }
  </code></pre>
  
  <p>CustomTelemetryInitializer.cs</p>
  
  <pre><code>public class CustomTelemetryInitializer : ITelemetryInitializer
  {
      private readonly string roleName;
  
      public CustomTelemetryInitializer(string roleName)
      {
          this.roleName = roleName;
      }
  
      public void Initialize(ITelemetry telemetry)
      {
          if (string.IsNullOrEmpty(telemetry?.Context?.Cloud?.RoleName))
          {
              telemetry.Context.Cloud.RoleName = roleName;
          }
      }
  }
  </code></pre>
  
  <p>SampleService.cs</p>
  
  <pre><code>public class SampleService : ISampleInterface
  {
      private TelemetryClient telemetryClient;
  
      public SampleService(TelemetryClient telemetryClient)
      {
          this.telemetryClient = telemetryClient;
      }
  
      public void TestAppInsights()
      {
          telemetryClient.TrackEvent("Sample Custom Event with init");
          telemetryClient.TrackTrace("Sample Custom Trace with init");
      }
  }
  </code></pre>
  
  <p>Function.cs</p>
  
  <pre><code>public class Function1
  {
      private ISampleInterface service;
      public Function1(ISampleInterface service)
      {
          this.service = service;
      }
  
      [FunctionName("Function1")]
      public async Task&lt;IActionResult&gt; Run(
          [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
          ILogger log)
      {
          log.LogInformation("C# HTTP trigger function processed a request with init.");
  
          this.service.TestAppInsights();
  
          string name = req.Query["name"];
  
          string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
          dynamic data = JsonConvert.DeserializeObject(requestBody);
          name = name ?? data?.name;
  
          string responseMessage = string.IsNullOrEmpty(name)
              ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
              : $"Hello, {name}. This HTTP triggered function executed successfully.";
  
          return new OkObjectResult(responseMessage);
      }
  }
  </code></pre>