I am using ConsoleLoggerProvider from Microsoft.Extensions.Logging (MEL) to write logs in .NET 6 console application.
I want my logs to include "class" and "method" records for each log entry, to know "from where" in my code the log message was written.
The only current way to do it without using third-party logging solutions like Serilog, is to use a built-in feature of "log scopes" in MEL.
So I end up writing code like:
public class MyClass {
ILogger _logger;
.... //Contructor, etc.
public void MyMethod()
{
using (_logger.BeginScope($"{nameof(MyClass)} => {nameof(MyMethod)}"))
{
...//actual logic is here
}
}
}
I observe a concept of "ExternalScopeProviders" in the MS documentation but it's completely undocumented and example-free. Is there are more "implicit" way of having the code-structure scopes set automatically? Or are we (as .NET 6/C# users) are bound to explicit use of using (_logger.BeginScope) in our code to achieve this?