✅ using Logging with ASP.NET
ASP.NET has a logging service. It looks way better than mine. I believe it gets set up here:
Where can i find/modify this method? How can i trigger my own logs?
66 Replies
you can inject that logger with
ILogger<TheTypeDoingTheLogging>
Logging in .NET Core and ASP.NET Core
Learn how to use the logging framework provided by the Microsoft.Extensions.Logging NuGet package.
ok, almost there. i figured out how to use the ILogger, but now i need to generate a new ILogger<MyType> logger and i cant figure out how to do that in a way that works...
it should Just Work
you don't need to generate that yourself, DI can figure out how to construct the service you're asking for
yes, i passed
app.Logger
into my process, but it logs as though is ASP.NET.
i think i explaned that badly...
My App has two processes running: an ASP.NET api and a discord bot. right now when my discord bot needs to log something it just prints to the console. this works, but ASP.NETS logging looks way better so i want to implement this for my discord bot. So somehow i need to generate an istance of ILogger<Bot>
that i can pass to my discord bot so it can log stuff and look nice.
oh, ist an ILogger.i'm confused, what setup do you have for both processes to log to the same console?
becasue i didnt know there was another option?
theres one console...
and do you actually need 2 processes compared to hosted services?
i dont know how to create a hosted service.
Background tasks with hosted services in ASP.NET Core
Learn how to implement background tasks with hosted services in ASP.NET Core.
you could run the bot portion as a hosted service in your ASP.NET Core application
that sounds more integrated at the least... i still want to create a seprate logger for the bot to more easily identify when the bot loggs somthing and when the API logs somthing.
right, all that is involved in creating a "separate" logger is injecting
ILogger<T>
where T is any type you want (but typically the type you're injecting the logger into)
This doesnt work
when i try and log somthing it dosnt do anything.
i don't know what that is, that's not what i'm recommending
then when you create that class through DI it will inject an appropriate instance for you
alternatively get it yourself with
serviceProvider.GetRequiredService<ILogger<MyClass>>()
there's a whole bunch of logging infrastructure that is set up for you in DI, but with your code you're basically creating an empty unconfigured logger so it won't do anything