C
C#•4mo ago
GooBad

Logging...

public DataService() {
this.Logger = LoggerFactory.Create(builder => {
builder.AddSimpleConsole();
builder.AddDebug();
builder.SetMinimumLevel(LogLevel.Information);
}).CreateLogger("DataService");
}
public DataService() {
this.Logger = LoggerFactory.Create(builder => {
builder.AddSimpleConsole();
builder.AddDebug();
builder.SetMinimumLevel(LogLevel.Information);
}).CreateLogger("DataService");
}
This is logger on my class, i want to configure the builder globally how do i do that? there is 3000 articles in google total spam of paid medium etc. documentation has 304909 of usless words but i just want to know how to use a simple pattern like in python logging.basicconfig(level=) that will configure the logging level for my whole application :/
8 Replies
Denis
Denis•4mo ago
Use Dependency injection? add logging to your service provider, configure it. Then in classes that need logging, simply inject it, e.g., via the constructor, or properties. In other cases you could access your DI provider's singleton instance and resolve the logger that way. Depends on what you have now, and what app you are developing. If its an ASP.NET Core app, then DI is built-in
GooBad
GooBad•4mo ago
its just NET and sorry i am coming from Python and i never did dependency injection ill take a look so Dependency Injection is just depeneding on interface not implementation ?> 🤣 thank you :Dl ol
Jimmacle
Jimmacle•4mo ago
at its core dependency injection just means that classes don't need to know how to create objects they depend on, they're provided by the code creating the class that typically turns into using DI libraries that are basically magic bags that you register types and implementations with and it just figures out which chasses need what when you want to instantiate them
GooBad
GooBad•4mo ago
thank you, do you know any "epic" and "simple" at the same time examples?
Denis
Denis•4mo ago
Is it a console app? Wpf? Winforms? Maui? Avalonia
GooBad
GooBad•4mo ago
winform
Denis
Denis•4mo ago
I use Community toolkit Ioc for dependency injection; however , that comes with their MVVM library which you probably do not want in a winforms project. That is for the modern UI frameworks. You can choose that, or some other DI library. Here are a few I know of: - autofac - ninject (not sure whether still maintained) - dryioc (should be very fast) You'd probably register your logging in Program.cs Please check the selected library docs for specific examples, they should be well documented. I can help with community toolkit; however, as I've mentioned, it comes with tools and features you don't need
Denis
Denis•4mo ago
Ioc - Community Toolkits for .NET
An introduction to the use of the IServiceProvider type through the Microsoft.Extensions.DependencyInjection APIs