.net core日志系统相关总结

多年的经验,日志记录是软件开发的重要组成部分。没有日志记录机制的系统不是完善的系统。在开发阶段可以通过debug附件进程进行交互调试,可以检测到一些问题,但是在上线之后,日志的记录起到至关重要的作用。本文讲解下日志系统的相关使用

前言

本节开始整理日志相关的东西。先整理一下日志的基本原理。

正文

首先介绍一下包:

1.Microsoft.Extengsion.Logging.Abstrations

这个是接口包。

2.Microsoft.Extengsion.Logging

这个是实现包

3.Microsoft.Extengsion.Logging.Console

这个是扩展包

代码如下:

 static void Main(string[] args) { IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile("appsettings.json",optional:false,reloadOnChange:true); var config = configurationBuilder.Build(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(p=>config); serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService(); var loggerObj = loggerFactory.CreateLogger("Default"); loggerObj.LogInformation(2021, "Default,now that is 2021"); var loggerObj2 = loggerFactory.CreateLogger("loggerObj"); loggerObj2.LogDebug(2021, "loggerObj,now that is 2021"); Console.ReadKey(); }

配置文件:

 { "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "Console": { "LogLevel": { "Default": "Information", "Program": "Trace", "loggerObj": "Debug" } } } }

结果:

首先是配置级别的问题,查看loglevel 文件:

 public enum LogLevel { /// Logs that contain the most detailed messages. These messages may contain sensitive application data. /// These messages are disabled by default and should never be enabled in a production environment. Trace, /// Logs that are used for interactive investigation during development.  These logs should primarily contain /// information useful for debugging and have no long-term value. Debug, /// Logs that track the general flow of the application. These logs should have long-term value. Information, /// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the /// application execution to stop. Warning, /// Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a /// failure in the current activity, not an application-wide failure. Error, /// Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires /// immediate attention. Critical, /// Not used for writing log messages. Specifies that a logging category should not write any messages. None, }

从上之下,依次提高log级别。

比如说设置了log 级别是Error,那么Debug、Information、Warning 都不会被答应出来。

那么就来分析一下代码吧。

AddLogging:

 public static IServiceCollection AddLogging(this IServiceCollection services, Action configure) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>))); services.TryAddEnumerable(ServiceDescriptor.Singleton>( new DefaultLoggerLevelConfigureOptions(LogLevel.Information))); configure(new LoggingBuilder(services)); return services; }

这里面给注册了ILoggerFactory和ILogger。然后设置了一个打印log的级别配置,LogLevel.Information,这个就是如果我们没有配置文件默认就是Information这种级别了。

configure(new LoggingBuilder(services)) 给我们的委托提供了一个LoggingBuilder的实例化对象。这个对象就是用来专门做扩展的,是解耦的一种方式。

 internal class LoggingBuilder : ILoggingBuilder { public LoggingBuilder(IServiceCollection services) { Services = services; } public IServiceCollection Services { get; } }

这个LoggingBuilder 类基本什么功能都没有,但是因为有了这样一个类,就可以作为扩展的标志了。

比如说上文的:

 builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole();

看下AddConfiguration:

 public static ILoggingBuilder AddConfiguration(this ILoggingBuilder builder, IConfiguration configuration) { builder.AddConfiguration(); builder.Services.AddSingleton>(new LoggerFilterConfig

以上就是.net core日志系统相关总结的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程