You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove filters for ILoggers created by customer DI #4345
We need to be less restrictive on filtering logging categories. Today if someone creates a logger that is not in one of our "accepted" lists, we'll filter it away. You can get around this by adding one or more filters to get those categories flowing again, but this shouldn't be necessary.
The below settings will allow any category that starts with "OrderService" to be logged at "Information" and above levels.
Reacted by Tom Faltesek, Ian Kemp, Gabriel Chaney, Tankatronic, Jonathan ANTOINE, Masayuki Ota, Jean-Sébastien Goupil, danielwoodhead, Matthew Lieder, Sebastian Godelet and 1 more
Brett Samblanet (@brettsam) Thanks for the host.json workaround. Is there a way to specify log levels differently for development vs production environments?
Brett Samblanet (@brettsam) when you say: The below settings will allow any category that starts with "OrderService" to be logged at "Information" and above levels. what is a "category"? I am experiencing this bug but I'm not quite sure what the workaround is? See my code below:
I've created an Azure Service Bus function. The function uses a DI container using .NET Core's own DI libraries.
The function entry point receives an ILogger instance and we see log entries made with this instance. However, any log entries made with any generic instances of ILogger<MyClass> never show up. I can't point to this exact timeframe, but a few months ago when this function was first deployed, log entries did show up correctly. In fact, these entries were used to debug an issue we had. This is how I know it worked at some point in the past.
In my function, I do setup my DI container like so ..
public class MySolution.Functions.Tasks.MyFunction1
{
private readonly IServiceProvider _serviceProvider;
public ProcessTaskFunction()
{
_serviceProvider = Startup.BuildServiceProvider();
}
[FunctionName("MyFunction1")]
public async Task Run(
[ServiceBusTrigger("%TopicName%", "%SubscriptionName%", Connection = "ServiceBusConnectionString")]
string serviceBusMsg,
IDictionary<string, object> userProperties,
ILogger log)
{
{
//this message does show up in AppInsights
log.LogInformation($"C# ServiceBus topic trigger function processed message: {serviceBusMsg}");
using var scope = _serviceProvider.CreateScope();
var dep1 = scope.ServiceProvider.GetRequiredService<IMyDependency1>();
//This dependency takes an instance of ILogger<IMyDependency1>.
//Any calles to ILogger<IMyDependency1>.LogInformation() don't show up in AppInsights of the Kudu file system logs
dep1.DoStuff();
}
}
}
public class Startup
{
public static IServiceProvider BuildServiceProvider()
{
var configRoot = GetConfigurationRoot();
var services = new ServiceCollection();
AddOptions(services, configRoot);
AddConfiguration(services, configRoot);
AddLogging(services, configRoot);
AddSqlDbContext(services);
AddScopedServices(services);
AddHttpClients(services, configRoot);
//added these three lines of code after viewing comment https://github.com/Azure/azure-functions-host/issues/4345#issuecomment-580211775
services.AddSingleton<IConfiguration>(sp => configRoot);
services.RemoveAll<IConfigureOptions<LoggerFilterOptions>>();
services.ConfigureOptions<Common.Logging.LoggerFilterConfigureOptions>();
return services.BuildServiceProvider(true);
}
private static void AddLogging(IServiceCollection services, IConfiguration configRoot)
{
services.AddLogging(builder =>
{
builder.AddConfiguration(configRoot);
builder.AddConsole();
builder.AddDebug();
});
}
//other functions omitted
}
At this point, I'm debating rewriting my function and DI container to pass around the ILogger instance received in the function entry method.
My solution was just to take the ILogger instance passed in when the function is called and then register it in DI. Then every component which needed logging would get an instance of the original ILogger
//YourFunction.cspublicstaticvoidRun(/*other parameters*/,ILoggerlogger){YourDependencyInjection.Create(logger)}//YourDependencyInjection.csprivatevoidCreate(ILoggerlogger,ServiceCollectionservices){services.AddScoped<ILoggerWrapper,LoggerWrapper>(builder =>newLoggerWrapper(logger))}//ILoggerWrapper.cs maybe defined in a different project/dll. Add whatever methods you needpublicinterfaceILoggerWrapper{voidLogInfo();voidLogError();}//LoggerWrapperpublicclassLoggerWrapper:ILoggerWrapper{publicLoggerWrapper(ILoggerlogger){//save the reference}}//DependentComponentService.cspublicclassDependentComponentService{publicDependentComponentService(ILoggerWrapperloggerWrapper){//save the reference}}
This worked for me. I can now see logs in AppInsights. I couldn't see them earlier.
Reacted by Casey Boyle, erikmf12 and Roy Cornelissen
Is this issue still being worked on? Logging shouldn't be this hard to set up. If I have to troubleshoot to get my logs working, that's time that I waste not working on the app.
I've added my namespace to the host.json and I'm still not seeing my custom logs.
These 'workarounds' are great but I think it's ridiculous how I need to add more code than what's in my whole program just to see logs. Are we going to get a proper solution to this or is this inconsistent host.json workaround all we get?
Reacted by Chris Barnett, Eric Labashosky, sandeepiiit, Ralph Jansen, Roy Cornelissen and kosa-gyula-77
I agree that it is so ridiculous. Although I set min log level to Information for my namespace in host.json, I get only Warning level logs from my function in the console output when debugging solution with Visual Studio.
I don't see any log at any level in Application Insights or Log stream on the Azure portal!
namespace MyFunctions;
public sealed class TestLoggerFunction(ILogger<TestLoggerFunction> logger)
{
[Function(nameof(TestLoggerFunction))]
[FixedDelayRetry(5, "00:00:10")]
public void Run([TimerTrigger("*/10 * * * * *")] TimerInfo timer)
{
logger.LogTrace("This is a trace-level log message.");
logger.LogDebug("This is a debug-level log message.");
logger.LogInformation("This is an information-level log message.");
logger.LogWarning("This is a warning-level log message.");
logger.LogError("This is an error-level log message.");
logger.LogCritical("This is a critical-level log message.");
}
}
The function is set up not to send logs directly to Application Insights (Microsoft.ApplicationInsights.WorkerService and Microsoft.Azure.Functions.Worker.ApplicationInsights packages are not used). It relies on the default behavior: custom application logs you write are sent to the Functions host, which then sends them filtered to Application Insights. Refer to https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#custom-application-logs
Only this approach is supported by Aspire.
Activity
sandeepiiit commentedon Jun 5, 2019
Brett Samblanet (@brettsam) Thanks for the host.json workaround. Is there a way to specify log levels differently for development vs production environments?
bchrisb commentedon Jun 19, 2019
Brett Samblanet (@brettsam) when you say:
The below settings will allow any category that starts with "OrderService" to be logged at "Information" and above levels.what is a "category"? I am experiencing this bug but I'm not quite sure what the workaround is? See my code below:And then my host.json:
I still don't get any logs. What am I missing?
bchrisb commentedon Jun 19, 2019
Brett Samblanet (@brettsam) sorry, I've just found the answer to my own question by looking at this: #4425 (comment)
12 remaining items
pragnagopa commentedon Oct 1, 2020
Adam Weigert (@smokedlinq) - Core Tools regression is fixed in the latest version: https://github.com/Azure/azure-functions-core-tools/releases/tag/3.0.2931 - if filter is still not working please open a separate issue here: https://github.com/Azure/azure-functions-core-tools/
smokedlinq commentedon Oct 1, 2020
Just confirmed, 3.0.2931 does behave as expected. Thanks all.
abjbhat commentedon Dec 14, 2020
I think I'm seeing this problem as well.
I've created an Azure Service Bus function. The function uses a DI container using .NET Core's own DI libraries.
The function entry point receives an ILogger instance and we see log entries made with this instance. However, any log entries made with any generic instances of
ILogger<MyClass>never show up. I can't point to this exact timeframe, but a few months ago when this function was first deployed, log entries did show up correctly. In fact, these entries were used to debug an issue we had. This is how I know it worked at some point in the past.The current function runtime is
3.0.15185.0So far, I have tried ...
In my function, I do setup my DI container like so ..
At this point, I'm debating rewriting my function and DI container to pass around the
ILoggerinstance received in the function entry method.Any help would be appreciated.
Thanks.
fix logging to appInsights
jonathanantoine commentedon Mar 26, 2021
I have the same issue thant abjbhat and I would love to have a workaround :(
hachanMSFT commentedon Dec 21, 2021
I am hitting the same issue, did you find a work around ? Jonathan ANTOINE (@jonathanantoine) abjbhat
abjbhat commentedon Dec 21, 2021
hachanMSFT Jonathan ANTOINE (@jonathanantoine)
My solution was just to take the
ILoggerinstance passed in when the function is called and then register it in DI. Then every component which needed logging would get an instance of the originalILoggerThis worked for me. I can now see logs in AppInsights. I couldn't see them earlier.
erikmf12 commentedon Feb 1, 2024
Is this issue still being worked on? Logging shouldn't be this hard to set up. If I have to troubleshoot to get my logs working, that's time that I waste not working on the app.
I've added my namespace to the host.json and I'm still not seeing my custom logs.
These 'workarounds' are great but I think it's ridiculous how I need to add more code than what's in my whole program just to see logs. Are we going to get a proper solution to this or is this inconsistent host.json workaround all we get?
kosa-gyula-77 commentedon Jan 8, 2025
I agree that it is so ridiculous. Although I set min log level to
Informationfor my namespace inhost.json, I get onlyWarninglevel logs from my function in the console output when debugging solution with Visual Studio.I don't see any log at any level in Application Insights or Log stream on the Azure portal!
host.json
csproj
The function is set up not to send logs directly to Application Insights (
Microsoft.ApplicationInsights.WorkerServiceandMicrosoft.Azure.Functions.Worker.ApplicationInsightspackages are not used). It relies on the default behavior: custom application logs you write are sent to the Functions host, which then sends them filtered to Application Insights. Refer to https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#custom-application-logsOnly this approach is supported by Aspire.