C
C#2w ago
hutonahill

✅ using Logging with ASP.NET

ASP.NET has a logging service. It looks way better than mine. I believe it gets set up here:
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
Where can i find/modify this method? How can i trigger my own logs?
No description
66 Replies
Jimmacle
Jimmacle2w ago
you can inject that logger with ILogger<TheTypeDoingTheLogging>
Jimmacle
Jimmacle2w ago
Logging in .NET Core and ASP.NET Core
Learn how to use the logging framework provided by the Microsoft.Extensions.Logging NuGet package.
hutonahill
hutonahill2w ago
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...
Jimmacle
Jimmacle2w ago
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
hutonahill
hutonahill2w ago
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.
Jimmacle
Jimmacle2w ago
i'm confused, what setup do you have for both processes to log to the same console?
hutonahill
hutonahill2w ago
becasue i didnt know there was another option? theres one console...
Jimmacle
Jimmacle2w ago
and do you actually need 2 processes compared to hosted services?
hutonahill
hutonahill2w ago
i dont know how to create a hosted service.
Jimmacle
Jimmacle2w ago
you could run the bot portion as a hosted service in your ASP.NET Core application
hutonahill
hutonahill2w ago
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.
Jimmacle
Jimmacle2w ago
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)
hutonahill
hutonahill2w ago
Logger<Bot> discordLogger = new Logger<Bot>(new LoggerFactory());

Bot discordBot = new Bot(botConfig, discordLogger);
taskList.Add(discordBot.Run());
Logger<Bot> discordLogger = new Logger<Bot>(new LoggerFactory());

Bot discordBot = new Bot(botConfig, discordLogger);
taskList.Add(discordBot.Run());
This doesnt work when i try and log somthing it dosnt do anything.
Jimmacle
Jimmacle2w ago
i don't know what that is, that's not what i'm recommending
public class MyClass
{
public MyClass(ILogger<MyClass> logger)
{
logger.LogInformation("Hi I'm Logging");
}
}
public class MyClass
{
public MyClass(ILogger<MyClass> logger)
{
logger.LogInformation("Hi I'm Logging");
}
}
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
Want results from more Discord servers?
Add your server