<commit_before>using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
using System;

namespace Lombiq.Hosting.Azure.ApplicationInsights.TelemetryInitializers;

internal class ShellNamePopulatingTelemetryInitializer : ITelemetryInitializer
{
    private readonly IServiceProvider _serviceProvider;

    public ShellNamePopulatingTelemetryInitializer(IServiceProvider serviceProvider) =>
        _serviceProvider = serviceProvider;

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is not ISupportProperties supportProperties) return;

        var httpContext = _serviceProvider.GetHttpContextSafely();
        if (httpContext != null)
        {
            var httpRequest = httpContext.Request;

            var shellName = _serviceProvider
                .GetService<IRunningShellTable>()
                ?.Match(httpRequest.Host, httpRequest.PathBase + httpRequest.Path, fallbackToDefault: true)
                ?.Name;

            if (!string.IsNullOrEmpty(shellName))
            {
                supportProperties.TryAddProperty("ShellName", shellName);
            }
        }
    }
}<commit_msg>Fixing analyzer violation<commit_after>using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
using System;

namespace Lombiq.Hosting.Azure.ApplicationInsights.TelemetryInitializers;

internal class ShellNamePopulatingTelemetryInitializer : ITelemetryInitializer
{
    private readonly IServiceProvider _serviceProvider;

    public ShellNamePopulatingTelemetryInitializer(IServiceProvider serviceProvider) =>
        _serviceProvider = serviceProvider;

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is not ISupportProperties supportProperties) return;

        var httpContext = _serviceProvider.GetHttpContextSafely();
        if (httpContext != null)
        {
            var httpRequest = httpContext.Request;

            var shellName = _serviceProvider
                .GetService<IRunningShellTable>()
                ?.Match(httpRequest.Host, httpRequest.PathBase + httpRequest.Path)
                ?.Name;

            if (!string.IsNullOrEmpty(shellName))
            {
                supportProperties.TryAddProperty("ShellName", shellName);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0682777138473341, 'fraction_numerical': 0.0, 'mean_word_length': 3.353923205342237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '137721', 'n_tokens_mistral': 644, 'n_tokens_neox': 623, 'n_words': 122}