.net core 日志框架:聊聊记日志的最佳姿势 电脑版发表于:2020/7/10 15:30 ![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 日志框架:聊聊记日志的最佳姿势 [TOC] <br/> 必要的依赖包 ------------ <br/> - Microsoft.Extensions.Configuration.Json - Microsoft.Extensions.Logging - Microsoft.Extensions.Logging.Console - Microsoft.Extensions.Logging.Debug - Microsoft.Extensions.Logging.TraceSource 内容示例 ------------ >###项目架构 ![](https://img.tnblog.net/arcimg/hb/dd2653edc21b489cb6e2e303cf025143.png) >###内容示例 <br/> >这里我们对日志程序做配置 <font style="color:#f1c40f;font-weight:bold;">appsetting.json</font> ```json { "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, //下面是针对 Console 的输出提供程序 "Console": { "LogLevel": { "Default": "Information", "Program": "Trace", "alogger": "Trace", "LoggingSimpleDemo.OrderService": "None" //None表示关闭日志级别 } } } } ``` >注意 appsetting.json表示对日志的级别定位,具体请参考LogLevel枚举 ![](https://img.tnblog.net/arcimg/hb/ff53bf868d5b478b815b051801d20a5f.png) ![](https://img.tnblog.net/arcimg/hb/dcbd6e6e6d914f108e1e07535fb8a796.png) ><font style="color:#2ecc71;font-weight:bold;">Program.cs</font> ```csharp static void Main(string[] args) { //从文件中读取配置 IConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); var config = configBuilder.Build(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton<IConfiguration>(p => config); //用工厂模式将配置对象注册到容器管理 serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService<ILoggerFactory>(); var alogger = loggerFactory.CreateLogger("alogger"); alogger.LogDebug(2001, "aidasi"); var alogger2 = loggerFactory.CreateLogger("alogger"); alogger2.LogDebug("aidasi"); var ex = new Exception("Happen an Error"); alogger.LogError(ex, "Happen an Error"); Console.ReadKey(); } ``` >运行结果 ![运行结果](https://img.tnblog.net/arcimg/hb/28c1f0d64d6e4a27bc960adae5f7c4bb.png) >一般我们可以通过注入的方式使用日志 <font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> ```csharp public class OrderService { ILogger<OrderService> _logger; public OrderService(ILogger<OrderService> logger) { _logger = logger; } public void Show() { //内部传递参数(可以节省一定的资源) _logger.LogInformation("Show Time {time}",DateTime.Now); //拼接字符串(都会去拼接一次) _logger.LogInformation($"Show Time {DateTime.Now}"); } } ``` ><font style="color:#2ecc71;font-weight:bold;">Program.cs</font> ```csharp class Program { static void Main(string[] args) { //从文件中读取配置 IConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); var config = configBuilder.Build(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton<IConfiguration>(p => config); //用工厂模式将配置对象注册到容器管理 serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); serviceCollection.AddTransient<OrderService>(); IServiceProvider service = serviceCollection.BuildServiceProvider(); var order = service.GetService<OrderService>(); order.Show(); Console.ReadKey(); } } ``` >运行结果 ![](https://img.tnblog.net/arcimg/hb/e6a0ad7252b840709af0c6c9922fd784.png) >我们发现这里什么也没有,因为在配置中我们把`"LoggingSimpleDemo.OrderService": "None"` >修改<font style="color:#f1c40f;font-weight:bold;">appsetting.json</font>为**Debug** >再次运行 ![](https://img.tnblog.net/arcimg/hb/a3123fbd1e0a4d94862a4b9d608330d4.png)