一 netcore日志原理
netcore的日志是作为一个扩展库存在的,每个组件都有它的入口,那么作为研究这个组件的入口是最好的,首先看两种方式:
这个是源码例子提供的。
View Code这个是咱们使用hostbuild中的扩展
View Code从以上两种可以看出,其实第二种WebHostBuilder是封装第一种的。所以咱们选择从第一个入口着手。

netcore日志设计思想是:LoggingBuider 构建,LoggerFactory和Logger类负责日志操作和Log提供程序的管理,Configuration是配置功能。
那么咱们基于以上的代码,看LoggingBuilder类
using Microsoft.Extensions.DependencyInjection; namespace Microsoft.Extensions.Logging { internal class LoggingBuilder : ILoggingBuilder { public LoggingBuilder(IServiceCollection services) { Services = services; } public IServiceCollection Services { get; } } }
再看为Service做的扩展
using System; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection { /// <summary> /// Extension methods for setting up logging services in an <see cref="IServiceCollection" />. /// </summary> public static class LoggingServiceCollectionExtensions { /// <summary> /// Adds logging services to the specified <see cref="IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> public static IServiceCollection AddLogging(this IServiceCollection services) { return AddLogging(services, builder => { }); } /// <summary> /// Adds logging services to the specified <see cref="IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> /// <param name="configure">The <see cref="ILoggingBuilder"/> configuration delegate.</param> /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> public static IServiceCollection AddLogging(this IServiceCollection services, Action<ILoggingBuilder>

