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

CustomDimensions from BeginScope missing when using ILogger #2918

Open
jamesmundy opened this issue Nov 16, 2024 · 0 comments
Open

CustomDimensions from BeginScope missing when using ILogger #2918

jamesmundy opened this issue Nov 16, 2024 · 0 comments

Comments

@jamesmundy
Copy link

jamesmundy commented Nov 16, 2024

I've been referred to post my issue here: Azure/azure-functions-dotnet-worker#2870

I am wanting to log some custom properties to my ILogger logs (Application Insights Traces) in Azure Functions Isolated however whatever I add is never present in the Application Insights logs and no customDimensions are saved.

My Program.cs:

.ConfigureServices((hostContext, services) =>
{
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
    services.AddLogging((ILoggingBuilder builder) =>
    {
        builder.AddSimpleConsole(x => x.IncludeScopes = true);
        builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);


        // Disable Informational logs from Azure SDK. These get wired up automatically when using AddAzureClients()
        // Docs -- https://learn.microsoft.com/en-us/dotnet/azure/sdk/logging#logging-with-client-registration
        builder.AddFilter("Azure.Core", LogLevel.Warning);
    });

The packages I'm using

    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" />

My code using BeginScope to record the variables:

public static class LoggerExtensions
{
    public static void LogCustomTrace(this ILogger logger,
        string method,
        string message,
        TraceType traceType,
        LogLevel logLevel = LogLevel.Information,
        Exception? exception = null)
    {
        using (logger.BeginScope(new Dictionary<string, object>
        { { "TraceType", traceType } }))
        {
            if (exception != null)
            {
                logger.Log(logLevel, exception, $"[{method}]: {message}");
            }
            else
            {
                logger.Log(logLevel, $"[{method}]: {message}");
            }
        }
    }
}

Investigative information

Please provide the following:

  • Timestamp:
    2024-11-14T16:02:01.0716062Z
  • Function App version: 4
  • Function App name: internalapi-func-auto-dev-001
  • Function name(s) (as appropriate):
  • Invocation ID:
    88a1715d-2c98-4482-bfb7-d40bcebcf050
  • Region: EU West

Repro steps

Use the code above

Expected behavior

the variable "traceType" should be present in the customDimensions.

Actual behavior

The customDimensions are like this:
{"ProcessId":"4224","HostInstanceId":"51952c6f-638d-4f8f-a4c3-b59d06b6b4c4","prop__{OriginalFormat}":"[ManageMachineNumbersAsync]: Running: 1 Creating: 0 Required: 1","LogLevel":"Information","Category":"Host.Function.Console"}

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

No branches or pull requests

1 participant