使用 .Net Core 控制台记录器记录事件日期时间 [英] Log event datetime with.Net Core Console logger

查看:53
本文介绍了使用 .Net Core 控制台记录器记录事件日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .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.LoggerExtensions 类和方法 Log...这是登录我的应用程序的示例:

Also for logging I'm using Microsoft.Extensions.Logging.LoggerExtensions class with methods Log... Here an example of logging in my App:

_logger.LogInformation(eventId, "Action is started.");

其中 _loggerILogger 类的实例,并在具有内置依赖注入的类构造函数中初始化.调用上述方法的结果控制台输出显示以下字符串:

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 方法时的时间,但似乎 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.

是否存在一些允许在控制台输出中显示操作日期时间而不将其作为消息的一部分传递给方法的方法或附加类格式化程序?

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 控制台记录器记录事件日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆