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

ApplicationInsightsLogger.IsEnabled always return true #3098

Open
hungnd1475 opened this issue Sep 25, 2024 · 0 comments
Open

ApplicationInsightsLogger.IsEnabled always return true #3098

hungnd1475 opened this issue Sep 25, 2024 · 0 comments

Comments

@hungnd1475
Copy link

Please provide a succinct description of the issue.

Repro steps

Provide the steps required to reproduce the problem

  1. Set default log level in host.json to Information
{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true
      }
    }
  }
}
  1. Inject an ILogger<ClassA> _logger into ClassA and call _logger.IsEnabled(LogLevel.Debug)
public class Argument
{
  public string Property1 { get; init; }
  public int Property2 { get; init; }
}

public class ClassA
{
  private readonly ILogger<ClassA> _logger;

  public ClassA(ILogger<ClassA> logger)
  {
    _logger = logger;
  }

  public void DoWork(Argument arg)
  {
    if (_logger.IsEnabled(LogLevel.Debug))
    {
      _logger.LogDebug("Begining DoWork with argument: {Argument}", JsonSerializer.Serialize(arg));
    }
    else
    {
      _logger.LogInformation("Beginning DoWork");
    }
    // ... do the work
  }
}

Expected behavior

The Information log message should show up in Application Insights.

Actual behavior

The Debug log message shows up in Application Insights

Known workarounds

Not that I know off.

Related information

Upon inspecting the source code of ApplicationInsightsLogger, I noticed that the method IsEnabled(LogLevel) always return true. It has a comment explaining why but I think it is ignoring the fact that many code depends on this method working correctly to improve logging performance, avoiding costly code path to be executed when turned off, like in the example above where in staging environment I'd set AzureFunctionsJobHost__logging__logLevel__default to Debug to get the serialized argument object. Even MS docs recommend that way (link).

If ILogger.IsEnabled is not reliable, are there alternative ways to achieve the above?

  • Package version
  • Links to source
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