.NET Core 和 ASP.NET Core 中的日志记录:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0
在 Get 方法中添加日志
WeatherForecastController.cs
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
_logger.LogInformation("Get action executed");
_logger.LogInformation(new EventId(1001, "Action"), "Get action executed");
根据不同的类名区分
private readonly ILogger<WeatherForecastController> _logger;
private readonly ILogger _myLogger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<WeatherForecastController>();
_myLogger = loggerFactory.CreateLogger("MyLogger");// 通过自己的分类 MyLogger 创建
}
LogLevel | Value | Method | Description | 推荐使用场景 |
---|---|---|---|---|
Trace | 0 | LogTrace | 跟踪日志:粒度细,非常详细跟踪日志,包括方法的进入结束。一般是用于sdk、或者一些基础设施上 | 开发环境/特殊环境 |
Debug | 1 | LogDebug | 调试日志:记录一些比较容易出错的一些跟踪信息 | 开发环境/特殊环境 |
Information | 2 | LogInformation | 信息:生产级别开启。相对来说比较重要的节点:比如订单支付成功、取消成功 | 生产 |
Warning | 3 | LogWarning | 警告:有一定错误,但不影响结果执行 | 生产 |
Error | 4 | LogError | 错误:导致程序不能正常往下执行业务的错误 | 生产 |
Critical | 5 | LogCritical | 致命:记录信息要求,系统崩溃 | 生产 |
None | 6 |
源码:https://github.com/aspnet/Logging/tree/master/src/
ILoggerProvider.cs
using System;
namespace Microsoft.Extensions.Logging
{
/// <summary>
/// Represents a type that can create instances of <see cref="ILogger"/>.
/// </summary>
public interface ILoggerProvider : IDisposable
{
/// <summary>
/// Creates a new <see cref="ILogger"/> instance.
/// </summary>
/// <param name="categoryName">The category name for messages produced by the logger.</param>
/// <returns></returns>
ILogger CreateLogger(string categoryName);
}
}
支持不同类型的日志输出,可以自定义一个 LoggerProvider
打印容器中所有注入的 LoggerProvider
Program.cs
var providers = host.Services.GetServices<ILoggerProvider>();// 获取容器中所有注入的实例
foreach (var provider in providers)
{
Console.WriteLine(provider.GetType().ToString());
}
启动程序,输出如下:
Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider
Microsoft.Extensions.Logging.Debug.DebugLoggerProvider
Microsoft.Extensions.Logging.EventSource.EventSourceLoggerProvider
Microsoft.Extensions.Logging.EventLog.EventLogLoggerProvider
添加,清除
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((ctx, logger) =>
{
//logger.AddProvider();// 添加
logger.ClearProviders();// 清除
})
先注释清除代码
//.ConfigureLogging((ctx, logger) =>
//{
// //logger.AddProvider();// 添加
// logger.ClearProviders();// 清除
//})
在 appsettings.json 调整日志级别为 Trace
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Trace",
"Microsoft.Hosting.Lifetime": "Trace"
}
}
}
在 WeatherForecastController 中添加一个方法,根据需求使用日志
public IActionResult CreateOrder(dynamic order)
{
_logger.LogTrace("Enter CreateOrder method");
_logger.LogDebug("Start creating order: {0}", "order info");
_logger.LogTrace("Start executing _orderService.Create method");
if (order.amount <= 0)
{
_logger.LogWarning("Order Amount is:{0}");
}
_orderService.Create(order);
_logger.LogTrace("Completed executing _orderService.Crete method");
_logger.LogTrace("Leave CreateOrder Successfully");
_logger.LogInformation("Leave CreateOrder Successfully");
return Ok();
}
https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp/HelloApi
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。