Stellen Sie die Ausgangsfarbe von Kestrel aus Ilogger ein
Posted: 17 Aug 2025, 21:26
I noticed when setting different logging levels in ASP.NET Core, that sometimes the logging output generated by different calls to Entity Framework among other things, the console ForeColor changes to yellow, red, blue, etc...
I thought it would be nice to be able to do custom colors to this window (just to make the stuff I am putting into the log output stand out a little better as a message sent to the ILogger from my Code) < /p>
Ich habe versucht, die Farbe auf diese Weise festzulegen (nur um zu sehen, ob es funktionieren würde) ... < /p>
//set the console color (expecting the Kestrel console window to change to this)
var oldColor = Console.ForeColor;
Console.ForeColor = ConsoleColor.Yellow
//send the log message (expecting it to be yellow when output in the kestrel console window)
_logger.LogTrace(@"Hello World!");
//be a good citizen and set the color back now that the log message has been displayed
Console.ForeColor = oldColor;
< /code>
Leider ist es nicht so einfach, ich muss dies entweder in der Fabrik tun, oder es gibt noch etwas in der Iloogger, die mich tun wird, um es zu tun, um hier zu fragen, bevor ich anfing, mich im Entitäts -Framework -Code herumzugraben, um zu sehen, wie sie (Microsoft). Also werde ich mich im Voraus entschuldigen, wenn es bereits beantwortet wurde und ich nur an den falschen Stellen gesucht habe.
Danke im Voraus!
I thought it would be nice to be able to do custom colors to this window (just to make the stuff I am putting into the log output stand out a little better as a message sent to the ILogger from my Code) < /p>
Ich habe versucht, die Farbe auf diese Weise festzulegen (nur um zu sehen, ob es funktionieren würde) ... < /p>
//set the console color (expecting the Kestrel console window to change to this)
var oldColor = Console.ForeColor;
Console.ForeColor = ConsoleColor.Yellow
//send the log message (expecting it to be yellow when output in the kestrel console window)
_logger.LogTrace(@"Hello World!");
//be a good citizen and set the color back now that the log message has been displayed
Console.ForeColor = oldColor;
< /code>
Leider ist es nicht so einfach, ich muss dies entweder in der Fabrik tun, oder es gibt noch etwas in der Iloogger, die mich tun wird, um es zu tun, um hier zu fragen, bevor ich anfing, mich im Entitäts -Framework -Code herumzugraben, um zu sehen, wie sie (Microsoft). Also werde ich mich im Voraus entschuldigen, wenn es bereits beantwortet wurde und ich nur an den falschen Stellen gesucht habe.
Danke im Voraus!