在ASP.NET Core中分离应用程序级别的日志记录和框架级别的日志记录 [英] Separating application level logging and framework level logging in ASP.NET Core

查看:460
本文介绍了在ASP.NET Core中分离应用程序级别的日志记录和框架级别的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将日志记录服务添加到容器(在ASP.NET 5 RC1中):

If I add logging services to container (in ASP.NET 5 RC1):

services.AddSingleton<ILoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
//or just services.AddLogging();

然后我可以在应用程序层中使用Logger:

Then I can use Logger in my app layer:

class MyAppLogicService
{
    public MyAppLogicService(ILogger<MyAppLogicService> logger) 
    { 
        logger.LogInformation("Hey");
    }
}

但是在这种情况下,我的logger.LogInformation()事件将与不重要框架信息事件混合在一起(根据每个请求最多10个开发者!).

But in this case my logger.LogInformation() events will mix with unimportant framework information events (according to devs up to 10 per request!).

ASP.NET 5文档状态:

建议您在以下级别执行应用程序日志记录: 您的应用程序及其API,而不是在框架级别.这 框架已经内置了日志功能,可以通过以下方式轻松启用该功能: 设置适当的日志记录详细级别.

It is recommended that you perform application logging at the level of your application and its APIs, not at the level of the framework. The framework already has logging built in which can be enabled simply by setting the appropriate logging verbosity level.

这是什么意思?这是否意味着不建议在客户端代码(应用程序逻辑)中使用ILogger/ILoggerFactory?

What does this mean? Does it mean that using ILogger/ILoggerFactory in client code (app logic) is not recommended?

将应用程序级别的日志记录与框架级别的日志记录分开的优雅解决方案是什么? 现在,我正在使用Serilog并通过ContextSource进行过滤,但这远非优雅……

What is an elegant solution to separate app level logging from framework level logging? For now I'm using Serilog and filtering by ContextSource, but this is far from elegant...

推荐答案

在应用程序及其API级别而不是框架级别执行应用程序日志记录 我认为这里的信息是您不应该尝试记录每个请求的详细信息,因为框架已经记录了该请求的详细信息.

perform application logging at the level of your application and its APIs, not at the level of the framework I think the message here is that you should not try and log every request details, because that is already logged by the framework.

从混合框架日志事件和应用程序日志事件开始-文档还指出:

As of mixing framework log events and application log events - the docs also states:

创建记录器时,必须提供类别名称.类别名称指定记录事件的来源.按照惯例,此字符串是分层的,类别之间用点(.)字符分隔.一些日志记录提供程序具有利用此约定的过滤支持,从而使查找所需的日志记录输出更加容易.

When a logger is created, a category name must be provided. The category name specifies the source of the logging events. By convention this string is hierarchical, with categories separated by dot (.) characters. Some logging providers have filtering support that leverages this convention, making it easier to locate logging output of interest.

默认情况下,使用注入的ILogger<MyAppLogicService>时,类别名称是类的全名(带有名称空间).

By default, when using injected ILogger<MyAppLogicService>, the category name if the full name of the class (with namespace).

因此,为避免使框架信息混乱,可以通过仅包含与名称空间匹配的类别来过滤掉所有干扰.使用ConsoleLogger时,它看起来像这样:

So, to avoid cluttering your log with framework information, you could filter out all the noise, by including only categories that match your namespace. When using ConsoleLogger it would look like this:

loggerFactory.AddConsole((cat, level) => cat.StartsWith("mynamespace."));

我想这类似于使用Serilog并通过ContextSource进行过滤".

I suppose this is similar to "using Serilog and filtering by ContextSource".

这篇关于在ASP.NET Core中分离应用程序级别的日志记录和框架级别的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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