ASP.NET Core 日志

内容纲要

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#nuget-packages

NuGet 包

ILogger 和 ILoggerFactory 接口位于 Microsoft.Extensions.Logging.Abstractions 中,其默认实现位于 Microsoft.Extensions.Logging 中。

日志类别

有两种指定方式,一种是类对象,一种是手动创建。
创建 ILogger 对象后,将为其指定“类别” 。 该类别包含在由此 ILogger 实例创建的每条日志消息中。 类别可以是任何字符串,但约定需使用类名,例如“TodoApi.Controllers.TodoController”。
使用 ILogger 获取一个 ILogger 实例,该实例使用 T 的完全限定类型名称作为类别:

public class TodoController : Controller
{
    private readonly ITodoRepository _todoRepository;
    private readonly ILogger _logger;

    public TodoController(ITodoRepository todoRepository,
        ILogger<TodoController> logger)
    {
        _todoRepository = todoRepository;
        _logger = logger;
    }

要显式指定类别,请调用 ILoggerFactory.CreateLogger:

public class TodoController : Controller
{
    private readonly ITodoRepository _todoRepository;
    private readonly ILogger _logger;

    public TodoController(ITodoRepository todoRepository,
        ILoggerFactory logger)
    {
        _todoRepository = todoRepository;
        _logger = logger.CreateLogger("TodoApiSample.Controllers.TodoController");
    }
点赞
  1. tadalafil cost说道:

    I am also writing to make you know what a impressive encounter my friend's girl gained viewing yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors' expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

tadalafil cost进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!