-
Notifications
You must be signed in to change notification settings - Fork 442
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
ILogger is not injected when using new DI functionality #4425
Comments
@alexzheludov Try asking for the type parameterized I used this example to reproduce it, but with |
It also doesn't work in my project. @anarsen you saying that if you place |
Thanks @anarsen, requesting @VaclavElias , yes, if you add |
Just to correct myself, it doesn't throw an exception in my case but it doesn't log anything. |
|
Yet it is working in ASP.NET Core. |
Tested it and got the same result. ILogger gets injected but doesn't log anything to application insights. I added logger to the SampleGreeter this way: public class SampleGreeter : IGreeter
{
private readonly ILogger<SampleGreeter> logger;
public SampleGreeter(ILogger<SampleGreeter> logger)
{
this.logger = logger;
}
public string CreateGreeting(string name)
{
logger.LogInformation("Logging from greeter");
return $"Hello, {name}. I'm a sample greeter.";
}
} And added logging statement to the function body: log.LogInformation("Logging from function"); Nothing is logged from greeter, while logging from function works. |
@older, @brettsam it is all very cryptic, isn't it? :) Finally, I made it working, tested locally only at the moment. All you did is ok but you need to white list SampleGreeter according this (till this is fixed) #4345. It didn't work for me just using SampleGreeter, I had to put there whole namespace Microsoft.Azure.Functions.Samples.DependencyInjectionBasic in your host.json
|
Yes, that is an unfortunate issue; sorry I forgot to point that out here. When we first built logging, we controlled all of the logging categories so we put a filter in place to weed out things coming from other components by default. Unfortunately it also weeds out custom categories, which is becoming more prevalent with DI opening up. I'll be addressing that soon so this filter can go away. |
@older -- Could you share how you got an I'm pretty sure they don't register an |
@brettsam My mistake. Just checked and you are right. Don't even know why I thought it was working... |
@brettsam @VaclavElias if your solution has common namespace, if you just reference it in the host.json, it will cover your whole solution.
I don't see any issues with having to supply this configuration, because we should be controlling our log levels regardless. If you enable trace logging in dev environment, you probably don't want to enable it in production. |
Thanks @alexzheludov that is helpful and I agree. I am also ok with white listing. Interestingly, once deployed when running manually it doesn't show the logs (except from functions) in the log-streaming But I can see the logs in Monitor, including from my services. Anyway, logging is back so I am ok now. |
Do I need to put a filter for every class that depends on the logger? Or can I get away with just specifying my project namespaces? |
In short, you can typically just put your namespace... it finds the longest matching prefix and uses that rule. The logging docs have some more complex examples: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#how-filtering-rules-are-applied |
@VaclavElias Did you find a solution for having the log information show up in the console when running the function manually? |
@GFoley83 nope, no solution found yet. |
According to this hidden gem: There is a regex on your namespaces. If your namespace starts with "Function", your log messages are pumped to the console. Otherwise you need to use the host file to configure the logging. related link Example: (I bet that using this in production is a bad idea 🤔 )
I hope this helps. |
If you're not seeing custom logger logs show up -- see this issue: #4345 |
The logs are coming through fine in App Insights, they're just not showing up in the log streaming service console when you run the Azure function manually in the Portal, as the screenshot shows here: It just logs the default function started, function finished messages. I've added the correct filtering on the When using the default |
Ah yes -- that's because the only way we know how to direct the logs to that window are via the logger's category. If it matches the form |
Hello Guys, Thanks to your advices above, I am able to log via a simple service instantiated via DI inside FunctionStartup (please see GetServiceRequestQueryHandler inside below code snippet). Most likely the problem is because I am creating an ILogger via a new LoggerFactory which is not the same as the Logger automatically injected inside the Function. I cannot let ServiceRequestService dependencies to be automatically resolved by the DI, gatewayService needs to be instantiated with the right settings (ie cosmodb connection string etc). Could you please advise what should be the right approach to have ILogger working inside my ServiceRequestService? Please find the code snippets below.
Thanks a lot, |
Have you tried |
@optiks brilliant, that fixed my issue! Thanks a lot! |
Setting the filter in the
to my startup class. This basicly shows logs from all projects in your solution that are referenced by the function. |
@fschaal I tried both host.json and also configuration from code, as you presented, but it does not change anything in my case. I put in a filter a common part of namespace that all solution's projects use and in both Monitoring and Azure Insights I still see only the messages that are logged by funcion's ILogger. |
@Loreno10 can you share your code that shows how you're using your loggers? I can try to reproduce it and see what the issue is. |
Thanks @brettsam I managed to fix it myself. I was registering my dependencies in my own IServiceCollection, instead of creating a Startup class. As soon as I switched to having Startup class and using IServiceCollection provided by it, logging started to work. Although still, not everywhere. Seems that it works only from classes that exist under my Azure Function project. When I log from classes that are in other projects (and that are referenced by Azure Function project), they are not visible in log stream. I put in host.json the common part of namespace that all my projects share. |
@Loreno10 According to this it sounds like logging should work for other referenced projects as well. No luck for me so far though. Anybody else have any luck? |
Anything that is using |
@brettsam I'm not getting any logs to app insights with this setup. I'm using generics on the service class to set the logger category. Service class in other project:
Function startup:
|
I tried this and it seemed to work for me -- see my gist to see if there's something I've set up differently: https://gist.github.com/brettsam/f0cb90806d7a9dff5d0aaa3068fa71bd. Note that when I call
In App Insights I query with:
And I can see the "Error" and "Information" logs. I do not see "Trace", as expected, as the filter only allows Information+ through. |
@brettsam The only difference I can spot is that my Service class is in another project that I'm referencing from the function project, but that shouldn't matter, right? Also, my T in this case is the Function class. Other than that all I should have to do is set the APPINSIGHTS_INSTRUMENTATIONKEY app setting, correct? |
Yup, that should be it. Can you share how you're writing your logs? Also, is there anything else important in your Startup.cs that could be affecting this? That may be worth sharing as well. |
Service.cs:
Function.cs:
In Startup.cs I'm doing the following in addition to adding
|
@brettsam I forgot to mention I'm also using Azure Gov cloud. Does the function host/runtime take that into account? Or do I need to somehow specify the full connection string to my app insights instance?
|
Ah. No, it does not. But you shouldn't be seeing any logs in App Insights in gov cloud. There's a DI-based workaround in this comment: Azure/azure-webjobs-sdk#2263 (comment). We have this added to the current sprint, so we hope to have this settable in the host.json soon. |
When using DI supplied with Function Extension nuget package, an instance of ILogger is not getting injected into dependent classes.
According to the article, we shouldn't have to setup our own logging, because functions have default application insights logging ("For Application Insights, Functions adds Application Insights automatically for you").
Is this intended functionality and article is inaccurate and we need to setup our logging in order to get ILogger to be injected, or is this a bug?
When resolving an instance of the Service, constructor is throwing an exception, because ILogger is not injected.
The text was updated successfully, but these errors were encountered: