.net core日志系统相关总结

首先介绍一下包:

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<IConfiguration>(p=>config); serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService<ILoggerFactory>(); 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" } } } }

结果:

.net core日志系统相关总结

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

public enum LogLevel { /// <summary>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.</summary> Trace, /// <summary>Logs that are used for interactive investigation during development. These logs should primarily contain /// information useful for debugging and have no long-term value.</summary> Debug, /// <summary>Logs that track the general flow of the application. These logs should have long-term value.</summary> Information, /// <summary>Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the /// application execution to stop.</summary> Warning, /// <summary>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.</summary> Error, /// <summary>Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires /// immediate attention.</summary> Critical, /// <summary>Not used for writing log messages. Specifies that a logging category should not write any messages.</summary> None, }

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

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

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

AddLogging:

public static IServiceCollection AddLogging(this IServiceCollection services, Action<ILoggingBuilder> configure) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>()); services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>))); services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<LoggerFilterOptions>>( 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<IConfigureOptions<LoggerFilterOptions>>(new LoggerFilterConfigureOptions(configuration)); builder.Services.AddSingleton<IOptionsChangeTokenSource<LoggerFilterOptions>>(new ConfigurationChangeTokenSource<LoggerFilterOptions>(configuration)); builder.Services.AddSingleton(new LoggingConfiguration(configuration)); return builder; }

这里面给我们注入了配置文件的配置:builder.Services.AddSingleton<IConfigureOptions>(new LoggerFilterConfigureOptions(configuration))

同时给我们注册监听令牌:builder.Services.AddSingleton<IOptionsChangeTokenSource>(new ConfigurationChangeTokenSource(configuration));

这里给我们注册配置保存在LoggingConfiguration中:builder.Services.AddSingleton(new LoggingConfiguration(configuration));

因为LoggingConfiguration 保存了,故而我们随时可以获取到LoggingConfiguration 的配置。

看下AddConsole:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfdzf.html