使用.Net Core Console记录器记录事件日期时间 [英] Log event datetime with.Net Core Console logger
问题描述
我正在使用日志记录到.Net Core框架内置的控制台输出。
记录器的初始化:
I'm using logging to Console output, that built-in to .Net Core framework. Here initialization of the logger:
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(new LoggerFactory()
.AddConsole());
也用于记录我正在使用 Microsoft.Extensions.Logging.LoggerExtension
的类,方法为 Log ...
以下是登录我的应用程序的示例:
Also for logging I'm using Microsoft.Extensions.Logging.LoggerExtension
s class with methods Log...
Here an example of logging in my App:
_logger.LogInformation(eventId, "Action is started.");
其中 _logger
是<$ c的实例$ c> ILogger< T> 类,并使用内置依赖项注入在类构造函数中初始化。
调用上述方法后,控制台输出显示以下字符串:
Where _logger
is instance of ILogger<T>
class and initialized in the class constructor with built-in dependency injection.
As result of calling of the above method Console output shows following string:
info: NameSpaceName.ClassName[eventId] Action is started.
我想在控制台输出中显示日期时间,它指向时间,即日志方法已执行,但Log ..方法似乎不包含任何允许显示日期时间的方法。
I would like to display date-time in the Console output, that points to time, when the Log method is executed, but it seems that Log.. methods don't contain any methods that allow to display date time.
是否存在某些方法或additioanl classes-formatters可以在控制台输出中显示操作日期时间而不将其作为消息的一部分传递给方法?
Does it exist some method or additioanl classes-formatters that allow to display the action datetime in console output without passing it to the method as part of the message?
推荐答案
内置.NET Core控制台记录器不记录日期时间。跟踪此问题以获取更多详细信息。最简单的解决方法是:
Built-in .NET Core console logger doesn't log date-time. Track this issue to get more details. The easiest workaround is:
logger.Log(LogLevel.Information, 1, someObj, null, (s, e) => DateTime.Now + " " + s.ToString());
我写了自定义控制台记录器可自动记录时间戳并执行其他有用的技巧:
I wrote a custom console logger to automatically log the timestamp and do other useful tricks:
[2017.06.15 23:46:44] info: WebHost[1] Request starting HTTP/1.1 GET http://localhost:6002/hc
这篇关于使用.Net Core Console记录器记录事件日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!