aja*_*987 13 logging ms-extensions-logging .net-core-2.0
我有一个简单的测试项目,尝试使用Microsoft Logging Extensions包测试NetCore 2.0.
我遇到的问题是,当我第一次运行我的应用程序时,它会按预期记录信息性消息.我遇到的奇怪行为是后续运行根本不会产生任何消息.
我的项目面向Net Core 2.0框架,并安装了以下NuGet包:
下面是我的示例代码我正在尝试使用以下日志:
using System;
namespace LoggingNetCore2
{
using Microsoft.Extensions.Logging;
class Program
{
static void Main(string[] args)
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole();
loggerFactory.AddDebug(); // <-- why is this needed for console logging?
var logger = loggerFactory.CreateLogger(typeof(Program));
logger.LogInformation("Hello, World!");
logger.LogTrace("trace");
logger.LogDebug("debug");
logger.LogWarning("warning");
logger.LogCritical("critical");
logger.LogError("errrrr");
//using (logger.BeginScope("MyMessages"))
//{
// logger.LogInformation("Beginning Operation...");
// logger.LogInformation("Doing something cool... please wait.");
// logger.LogInformation("Completed successfully.");
//}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的操作时,我在控制台窗口上没有输出.任何可能会发生什么的想法都会浮现在脑海中?
我试过的事情:
编辑:如果我将调试日志记录提供程序添加到组合中,我现在在控制台窗口中显示日志.
在NetCore 1.x应用程序中,只需添加控制台提供程序即可.
编辑#2:关闭控制台日志记录提供程序不会像在net-core-1.x版本中那样立即将消息刷新到控制台.它似乎在不同的线程上运行.有关信息,请访问此网页:https://github.com/aspnet/Logging/issues/631
@ajawad987,你是对的。作品Dispose()
。
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection()
.AddLogging(config => config.AddConsole())
.BuildServiceProvider();
services.GetRequiredService<ILogger<Program>>()
.LogCritical("Hello");
((IDisposable) services)?.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3962 次 |
最近记录: |