C# System.Diagnostic.Debug.WriteLine不';不能在ASP.NET5WebAPI中工作 环境
我正在Mac上使用ASP.NET 5 1.0.0-rc1-update1和Visual Studio代码编写一个Web API。我使用YeomanC# System.Diagnostic.Debug.WriteLine不';不能在ASP.NET5WebAPI中工作 环境,c#,debugging,logging,asp.net-core,dnx,C#,Debugging,Logging,Asp.net Core,Dnx,我正在Mac上使用ASP.NET 5 1.0.0-rc1-update1和Visual Studio代码编写一个Web API。我使用Yeomangenerator aspnet构建了默认的Web API应用程序。我正在使用DNX Mono通过默认的web命令运行应用程序 System.Diagnostics.Debug中缺少输出 我想将调试输出记录到终端或Visual Studio代码输出。我试图使用System.Diagnostics.Debugclass来实现这一点,但是上面的代码产生零输
generator aspnet
构建了默认的Web API应用程序。我正在使用DNX Mono通过默认的web
命令运行应用程序
System.Diagnostics.Debug中缺少输出
我想将调试输出记录到终端或Visual Studio代码输出。我试图使用System.Diagnostics.Debug
class来实现这一点,但是上面的代码产生零输出
System.Disagnostics.Debug.WriteLine("This is your message");
我错过了什么?我需要在某处声明DEBUG
符号才能查看调试输出吗
Microsoft.Extensions.Logging.Debug.DebuggLogger中缺少
我还尝试了Microsoft.Extensions.Logging.Debug
软件包提供的功能,但没有成功。我想这是有道理的,因为它是System.Diagnostics.Debug
之上的一个包装器。我的Startup.cs
配置如下所示:
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddDebug(LogLevel.Debug);
// Configuring other middleware
}
我在控制器中创建了ILogger
,如下所示:
public ProductsController(ILoggerFactory logFactory)
{
_logger = logFactory.CreateLogger(nameof(ProductsController));
}
public IActionResult Ping()
{
_logger.LogDebug("Debug message");
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// This line can be removed if you are using 1.0.0-rc2 or higher
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
并使用如下ILogger
:
public ProductsController(ILoggerFactory logFactory)
{
_logger = logFactory.CreateLogger(nameof(ProductsController));
}
public IActionResult Ping()
{
_logger.LogDebug("Debug message");
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// This line can be removed if you are using 1.0.0-rc2 or higher
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
你必须加上一个TraceListener
TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);
另外,在Visual Studio项目属性中设置DEBUG
标志虽然@Martin answers完全解决了这个问题,但我想详细介绍一下在ASP.NET 5中记录DEBUG
输出
使用System.Diagnostics.Debug
System.Diagnostics.Debug.Listeners
默认情况下只有System.Diagnostics.DefaultTraceListener
,它似乎不会写入控制台。如果您添加一个控制台侦听器,如@Martin在其回答中建议的,控制台中将开始显示System.Diagnostics.Debug
和Microsoft.Extensions.Logging.Debug.DebugLogger
消息。对于后者,您还需要为构建指定DEBUG
符号
值得注意的是,System.Diagnostics.Debug.Listeners
属性和System.Diagnostics.TextWriterTraceListener
在核心CLR 5.0中不可用
使用Microsoft.Extensions.Logging.Console.ConsoleLogger
最初,我尝试在ASP.NET 5中将消息记录到控制台。MyloggerFactory
的配置如下:
public ProductsController(ILoggerFactory logFactory)
{
_logger = logFactory.CreateLogger(nameof(ProductsController));
}
public IActionResult Ping()
{
_logger.LogDebug("Debug message");
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// This line can be removed if you are using 1.0.0-rc2 or higher
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
我尝试使用\u logger.LogDebug(“调试消息”)记录调试消息代码>。它不起作用,导致我转而尝试System.Diagnostics.Debug
问题在于,ASP.NET 5 v1.0.0-rc1-update1除了配置特定记录器时提供的日志级别(例如,loggerFactory.AddConsole(LogLevel.Debug)
)之外,还使用了ILoggerFactory.MinimumLevel
)。如上所述
最低级别表示任何低于指定最低级别的日志
无论伐木工人多么努力,他们都不会看到
该值在1.0.0-rc1-update1中高于Debug
。因此,除非将ILoggerFactory.MinimumLevel
设置为LogLevel.Debug
,否则不会看到任何调试输出。请参阅报告的类似问题
看起来有点困惑,对吧?好消息是ASP.NET 5团队已经修复了这个“问题”和1.0.0-rc2中的ILoggerFactory.MinimumLevel
属性
如果仍在使用1.0.0-rc1-update1,则需要按如下方式配置日志记录:
public ProductsController(ILoggerFactory logFactory)
{
_logger = logFactory.CreateLogger(nameof(ProductsController));
}
public IActionResult Ping()
{
_logger.LogDebug("Debug message");
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// This line can be removed if you are using 1.0.0-rc2 or higher
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(LogLevel.Debug);
// Configuring other middleware
}
只要您通过日志api编写消息,调试记录器就应该工作。这就是你在做的吗?@VictorHurdugaci,不完全是。我试图直接使用System.Diagnostics.Debug.WriteLine
DebuggLogger
本质上是它的包装器,对吗?