-
Notifications
You must be signed in to change notification settings - Fork 451
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
Use scopes to direct logs to correct file #4689
Comments
Is there a temp work-around I could use to get the logs to show up in the log-streaming console window of the Azure function? #4425 (comment) |
@brettsam Can you confirm if there is any workaround? |
I just wonder how such bugs get to production at Microsoft? Didn't anyone try using logger with DI? Don't you have single unit-test checking that logs messages get to destinations using default setup? I had to inject ILoggerFactory instead of ILogger<> into constructors and initialize logger manually with name which starts with "Functions.": |
@SashaPshenychniy -- are you trying to log to a specific function file? If not, you're likely hitting #4345. There's a workaround listed there -- explicitly let your category through our filter. For others trying to log to the streaming logs for a specific function. The current ways you can get your logs to show up there are:
If that doesn't work (say you need a different specific category for this logger), that will have to wait until we can get this issue fixed. |
@brettsam Any update? Is this being actively developed? What's the release plan for this? |
This is really annoying bug/stupid feature. If you use injected services and you want to see logs in those services you have to pass Ilogger instance form function to every method. Please fix this. |
I am at serious loss of words on how this made into production and then ignored for over an year. Do Microsoft not use Azure Functions? |
@SinghManvir Somebody can correct me, but custom dependency injection was not supported in earlier versions of azure functions. So having ILogger injected into the specific function was the correct way how to do logs in azure functions, but azure functions involved and unfortunately some parts of the code/functionality missed the train. |
@brettsam 's workaround works for me, but really I shouldn't be doing: _logger = loggerFactory.CreateLogger(Microsoft.Azure.WebJobs.Logging.LogCategories.CreateFunctionUserCategory("MyFunctionClassName")); This breaks the very concept of Dependency Injection. Is there a fix for this? |
How do you use this "workaround", if you have for example a helper class which is used in multiple functions? I noticed the |
I feel the current need to us ILoggerFactory and LogCategories.CreateFunctionUserCategory with DI rather than ILogger should be clearly documented at https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library?tabs=v2%2Ccmd#logging. Googling and Microsoft Docs leads you down the path of configuring host.json log level settings, rather than this guidance on how better to inject a logger. |
Still no update? |
any update? any timeline for a fix? |
Microsoft has become a marketing company of lately . It has ceased to be a tech company it seems. |
@KapilGBunnings I think Microsoft is working on a successor to Azure Functions:
If I'm not mistaken, the github repo is https://github.com/Azure/azure-functions-dotnet-worker but I have not looked at it yet. Maybe it's been fixed there? I really don't know. |
Just add this in a code file to your project, and it will Just Work ™️ after adding the relevant [assembly: FunctionsStartup(typeof(LoggingStartup))]
public class LoggingStartup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Replace ILogger<T> with the one that works fine in all scenarios
var logger = builder.Services.FirstOrDefault(s => s.ServiceType == typeof(ILogger<>));
if (logger != null)
builder.Services.Remove(logger);
builder.Services.Add(new ServiceDescriptor(typeof(ILogger<>), typeof(FunctionsLogger<>), ServiceLifetime.Transient));
}
class FunctionsLogger<T> : ILogger<T>
{
readonly ILogger logger;
public FunctionsLogger(ILoggerFactory factory)
// See https://github.com/Azure/azure-functions-host/issues/4689#issuecomment-533195224
=> logger = factory.CreateLogger(LogCategories.CreateFunctionUserCategory(typeof(T).FullName));
public IDisposable BeginScope<TState>(TState state) => logger.BeginScope(state);
public bool IsEnabled(LogLevel logLevel) => logger.IsEnabled(logLevel);
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
=> logger.Log(logLevel, eventId, state, exception, formatter);
}
} |
This is still biting people 3 years later... @brettsam any updates? |
To articulate the reasons I'm unhappy with this:
Note if you try to be clever and use Edit: Actually, |
@IronSean It looks like Microsoft addressed this issue with .NET out-of-process Azure Functions: "Logging ... ILogger obtained from FunctionContext" So I think that's probably the way to go, as I was speculating in my comment on Jan 5. |
@m-ghaoui Yes, but we have to wait until the .NET 7 release as out-of-process Functions are not as feature-rich as in-process ones. |
4 years later and I'm still wasting hours of my time trying to get basic stuff to work because your own project templates do not work properly. I am not happy. |
Today the FileLogger (what you see in the portal when invoking functions) figures out the correct file to write to based on the
Function.{FunctionName}.User
standard category that we use. However, we should be able to look at the scope as well to see if there's a function name. If so, write there.The text was updated successfully, but these errors were encountered: