asp.net core mcroservices 架构之 分布式日志(二)之自定义日志开发

 

一   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>
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信