Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Diagnostic.Debug.WriteLine不';不能在ASP.NET5WebAPI中工作 环境_C#_Debugging_Logging_Asp.net Core_Dnx - Fatal编程技术网

C# System.Diagnostic.Debug.WriteLine不';不能在ASP.NET5WebAPI中工作 环境

C# 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来实现这一点,但是上面的代码产生零输

我正在Mac上使用ASP.NET 5 1.0.0-rc1-update1和Visual Studio代码编写一个Web API。我使用Yeoman
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中将消息记录到控制台。My
loggerFactory
的配置如下:

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
本质上是它的包装器,对吗?