-
Notifications
You must be signed in to change notification settings - Fork 63
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
Understanding why this happened. #49
Comments
You have to use FileLoggerProvider in the same way as other standard logging providers as described here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/ If loggers are used according to official MS guidelines they work efficiently. Normally you have only one LoggerFactory instance per application, and if you use standard 'host' infrastructure to maintain DI / Logging / Configurations you don't need to create LoggerFactory implicitely as Host does that for you under the hood, see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host Note that FileLoggerProvider implementation doesn't write messages to the file when you call ILogger methods; for efficiency purposes all messages goes into internal queue that is processed in a separate thread, in the same way as standard Console logger does. You can add several FileLoggerProvider instances and write them to, say, app.txt and app_debug.txt. To skip "Debug" messages for app.txt you can specify minimal logging level for this concrete FileLoggingProvider (by specifying "MinLevel"). From my experience, questions about log levels routing often occur when developer don't understand how Microsoft Logging infastructure works (so I may recommend only to re-read https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/ again). |
Regarding
makes sense, I've just added simple MVC Core example with 2 file loggers: https://github.com/nreco/logging/tree/master/examples/TwoLogFilesMvc |
Thank you for your information. I have read over you code for your examples. I do not understand how the public Startup(... env) method is getting called. In my code I have put all the LoggerFactory Logger information in a constructor. This makes it possible only to run the LoggerFactory once. I noticed that you put your logging code inside a controller. Is that the recommended route? Thank you, |
This is standard MVC Core app infrastructure, Startup class is registered in Program.cs with
See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/#create-logs and https://learn.microsoft.com/en-us/dotnet/core/extensions/logging |
I must be missing something. I have tried using the examples in the two links you used. The problem is that the _logger never gets set up. the public MyClass(ILogger logger) never gets called. This means that my _logger.message call fail. |
Example works, isn't it? ILogger is resolved by DI -- but this works only if you get MyClass instance via DI (DI is also a part of standard MVC Core infrastructure). |
The sample works, just not in my application since it is not a MVC Core application. Since this is a API.Net app the process would be different. |
Microsoft.Extensions.Logging is normally used with a standard DI infrastructure; and your console application can use a Generic Host that configures all these standard things (DI & logging). This is how Microsoft recommends to use their standard infrastructure components. You can use logging without DI and even without ILoggingBuilder - in the same way how FileLoggingProvider tests do that (via ILoggingFactory, and consume ILogger instances created implicitly in your code). However, in this case you have to explore how Microsoft.Extensions.Logging works under the hood to avoid misconfiguration and improper use. |
How do I log to my services.AddLogging file?
So I am trying to put logging into an app of mine. In my Program.cs file I created a service.AddLogging file with a name of app_debug.txt.
Being that I am new at using this package. I used the LoggingTests as my examples and created a new LoggingFactory and AddProvider with the file name app.txt. Created a new logger and then build methods to write to the log file (Start, End, Message and Exception).
When I run the app from a browser I get all the messages from my debug window in VS written to the app_debug.txt file. All the other messages are written to app.txt.
So I am wondering whether creating the factory and logger then disposing of the factory for every message call is going to cause problems if I have 300 people working on the app?
Questions?
One suggestion. This would be easier if there was some examples of how to use the package.
Thank you,
Michael
The text was updated successfully, but these errors were encountered: