我注意到在 ASP.NET Core 中设置不同的日志记录级别时,有时通过对 Entity Framework 的不同调用生成的日志记录输出等,控制台 ForeColor 更改为黄色、红色、蓝色等...
我认为能够对此窗口进行自定义颜色会很好(只是为了使我放入日志输出的内容更好地作为从我的代码发送到 ILogger 的消息)
我尝试以这种方式设置颜色(只是为了看看它是否会起作用)......
//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 = Console.ForeColor;
不幸的是,这不是那么简单,我必须在工厂中执行此操作,或者 ILogger 中的其他内容可以让我执行此操作 - 我想我会在这里询问,然后再开始在 Entity Framework Core 代码中进行挖掘以查看他们(微软)是如何做到的。
我尝试在 SO 和 MSDN 上搜索有关此主题的一些信息 - 我没有想出任何东西,所以如果它已经得到回答并且我只是在错误的地方寻找,我会提前道歉。
提前致谢!