ASP.Net Core日志记录和DebugView.exe [英] ASP.Net Core Logging and DebugView.exe

查看:59
本文介绍了ASP.Net Core日志记录和DebugView.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用DebugView.exe在经典的ASP.Net中查看来自Web应用程序的调试消息.我只需要使用Debug.WriteLine("Message to display");它显示在DebugView的窗口中.我还可以使用该方法在生产服务器上跟踪/查看消息.
例如.

无论如何,是否可以在ASP.Net Core的DebugView中查看那些日志记录消息?还是要摆脱ASPNet.Core中的这些额外日志记录?

我确实尝试在Startup.cs中排除了这些行...但还没有成功.

  loggerFactory.AddConsole((cat,level)=> cat.StartsWith("WindowsAuthentication.")&& level == LogLevel.Debug);loggerFactory.AddDebug(LogLevel.Debug); 

解决方案

如果附加了Debugger,则看起来源仅调用 Debug.WriteLine ,这意味着它在生产中是无操作的(除非您要使用调试器进行远程调试).请参阅GitHub上的相关源代码行,网址为 Eg. http://woutercx.com/2013/08/23/debugview-tool-how-it-saved-my-day/

In ASPNetCore, I am trying to do the samething by using the Logger. I can view all the loggings correctly in VS2015 Output Window. But I couldn't see it anymore in DebugView software. And those logging messages are missing with other thousand of logging lines from MVC & ASP Engines and it's really difficult to view the messages which I only want to view.

Please see as an example in the picture below:

Is there anyway to view those logging messages in DebugView in ASP.Net Core? Or anyway to get rid of those extra loggings in ASPNet.Core?

I did try to exclude those lines in Startup.cs... but no success yet.

loggerFactory.AddConsole((cat, level) => cat.StartsWith("WindowsAuthentication.") && level == LogLevel.Debug);
loggerFactory.AddDebug(LogLevel.Debug);

解决方案

It looks like the source only calls Debug.WriteLine if the Debugger is attached, meaning it is a no-op in production (unless you're remote debugging with a debugger attached). See the relevant source lines on GitHub at https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Debug/DebugLogger.cs#L48-L50. It says:

    public bool IsEnabled(LogLevel logLevel)
    {
        // If the filter is null, everything is enabled
        // unless the debugger is not attached
        return Debugger.IsAttached &&
            logLevel != LogLevel.None &&
            (_filter == null || _filter(_name, logLevel));
    }

I suspect you could write your own Debug Logger Provider that looks almost identical to what is in GitHub but have the IsEnabled method disregard the Debugger.IsAttached check.

这篇关于ASP.Net Core日志记录和DebugView.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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