-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Commiting batch of log messages conditionally. #38045
Comments
Have you tried using scopes, I feel like that could help group all logs for a certain request into one. And also for filter set configure the log level in appsettings.json |
@maryamariyan But scope does not affect filtering, does it? So if log level is set to |
I was recently thinking of a similar use-case as well. As long as nothing interesting (bad) happens, I don't need to log adjacent messages not meeting the level criterium. However when something interesting happens, I'd like to get all the adjacent messages -- even those below the level criterium. Take the following pseudo-code: var provider = new ConsoleLoggerProvider(LogLevel.Warning);
var logger = provider.CreateLogger(...);
using (logger.Scope("Handling request X"))
{
using (logger.Scope("Performing Authn"))
{
logger.LogDebug("Authenticated as Y");
}
using (logger.Scope("Performing Authz"))
{
logger.LogError("User has no permission!");
}
} Currently only the What I would like in this contrived example is to record everything that was captured within the outer scope, as there was some message that did meet the level criterium. So even though |
I have been wondering about whether ILogger.Log<TState> is allowed to save its parameters to a queue, return, and have a different thread examine the TState instance or invoke the formatter delegate later.
ConsoleLogger.Log invokes the formatter delegate and then sends the resulting string to the queue. This logging is not conditional per batch though, so it never needs to waste time formatting log entries that would be discarded later. In Microsoft.Extensions.Logging.AzureAppServices, BatchingLogger.Log behaves likewise. |
Ah, you're right, scope is not what you need. The closest thing I can think of that could help with this is to setup filter rules in appsettings.json file and then update it later during the lifetime of the application as needed.
|
The Another problem is that there is no standard way to report the original time stamp of the log entry to the provider's Batching seems much easier to implement as part of a logging provider, where it would know exactly which properties of |
According to #57803 (comment), it is not allowed. |
That would be useful feature |
IBufferedLogger and BufferedLogRecord were added for this purpose in .NET 9.0. If a logger provider implements IBufferedLogger in the same class as ILogger, then a log buffer can use that to write a series of buffered log records when the buffer is being flushed. Some logger providers support IBufferedLogger already, but not all. This was proposed in #104129 and implemented in #103138. The missing piece is the log buffer itself and the logic for when to flush the buffer. That API is being proposed in dotnet/extensions#5123 and implemented in dotnet/extensions#5635. Unlike IBufferedLogger, this part looks like it won't be supported on .NET Framework, even though it may be technically compatible. If the upcoming implementation in dotnet/extensions is not satisfactory, then you can instead implement a custom ILoggerFactory that takes care of the buffering and flushes the buffers to IBufferedLogger when requested. That isn't a trivial job, though. Challenges:
|
When running in production environment I'd like to have few log messages, if nothing terrible happens.
But in case of failure I need to get all the details and how request was processed.
The problem is that each log entry is independent, and log level is configured on application start.
Describe the solution you'd like
Why not let logger have additional mode of operation?
For example:
I can have as many
LogTrace
calls as I want in my code, but these messsages won't appear in log unless I callLogError
, which is equivalent to dynamically switching toTrace
log level in case of error.This mode can affect performance however, because all messages should be kepr in memory until batch is completed.
The text was updated successfully, but these errors were encountered: