zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#if !NET45
using Prow.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using System;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 文本日志配置服务注入方法扩展类
    /// </summary>
    public static class FileLoggerFactoryExtensions
    {
        #region ILoggerBuilder Interface
        /// <summary>
        /// 添加文件日志功能到服务容器中
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configure"></param>
        public static ILoggingBuilder AddFileLogger(this ILoggingBuilder builder, Action<FileLoggerOptions>? configure = null)
        {
            builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>();
            builder.Services.AddSingleton<IConfigureOptions<FileLoggerOptions>, LoggerProviderConfigureOptions<FileLoggerOptions, FileLoggerProvider>>();
            builder.Services.AddSingleton<IOptionsChangeTokenSource<FileLoggerOptions>, LoggerProviderOptionsChangeTokenSource<FileLoggerOptions, FileLoggerProvider>>();
            builder.Services.RegisterExceptionManager();
            builder.Services.Configure<FileLoggerOptions>(op =>
            {
                op.FileName = op.FileName.GetOSPlatformPath();
                configure?.Invoke(op);
            });
            return builder;
        }
        #endregion
    }
}
#endif