hwh
2024-07-11 9895abdb4c0c060c52e285631bf3df845efb31e7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog.Events;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog.Filters;
using Autofac.Core;
using Serilog.Formatting.Json;
using System.Linq;
using ZXing;
using SqlSugar.Extensions;
 
namespace Utility
{
    public static class ServiceCollectionExtensions
    {
        internal static string LogFilePath(string fileName) => $@"Logs/{fileName}/{DateTime.Now.Year}.{DateTime.Now.Month}/log_.log";
        internal static readonly string seriCustomProperty = "seriPos";
        internal static readonly string logOutputTemplate = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
        internal static readonly long? fileSize = 31457280L;
        const string template = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
        /// <summary>
        /// 添加配置Serilog
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IServiceCollection AddConfigSerilog(this IServiceCollection services)
        {
            // 创建Serilog记录日志
            //Log.Logger = new LoggerConfiguration()
            //    .MinimumLevel.Debug()
            //    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Fatal)
            //    .MinimumLevel.Override("Quartz", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Serilog", LogEventLevel.Information)
            //    // 全部日志写入到Console
            //    .WriteTo.Console()
            //    .WriteTo.Async(c => c.Console(
            //       theme: AnsiConsoleTheme.Literate,
            //        outputTemplate: template))
            //    // Debug日志写入到文件
            //    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug))
            //    .WriteTo.Async(c => c.File(
            //        path: "Logs/Debug_.txt",
            //        rollingInterval: RollingInterval.Day,
            //        fileSizeLimitBytes: 1024 * 1024 * 10,
            //        retainedFileCountLimit: 100,
            //        outputTemplate: template,
            //        restrictedToMinimumLevel: LogEventLevel.Debug))
            //    // Information日志写入到文件
            //    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information))
            //    .WriteTo.Async(c => c.File(
            //        path: "Logs/Information_.txt",
            //        rollingInterval: RollingInterval.Day,
            //        fileSizeLimitBytes: 1024 * 1024 * 10,
            //        retainedFileCountLimit: 100,
            //        outputTemplate: template,
            //        restrictedToMinimumLevel: LogEventLevel.Information))
            //    // Warning日志写入到文件
            //    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning))
            //    .WriteTo.Async(c => c.File(
            //        path: "Logs/Warning_.txt",
            //        rollingInterval: RollingInterval.Day,
            //        fileSizeLimitBytes: 1024 * 1024 * 10,
            //        retainedFileCountLimit: 100,
            //        outputTemplate: template,
            //        restrictedToMinimumLevel: LogEventLevel.Warning))
            //    // Error日志写入到文件
            //    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error))
            //    .WriteTo.Async(c => c.File(
            //        path: "Logs/Error_.txt",
            //        rollingInterval: RollingInterval.Day,
            //        fileSizeLimitBytes: 1024 * 1024 * 10,
            //        retainedFileCountLimit: 1000,
            //        outputTemplate: template,
            //        restrictedToMinimumLevel: LogEventLevel.Error))
            //    .CreateLogger();
 
            //Log.Logger = new LoggerConfiguration()
            //                .Enrich.FromLogContext()
            //                .WriteTo.Console() // 输出到控制台
            //                .MinimumLevel.Debug() // 测试最小记录级别
            //                .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => {
            //                    var result = p.Properties.FirstOrDefault(c => c.Key == "SourceContext").Value;
            //                    if (result != null)
            //                    {
            //                        return result.ToString().Contains("模块名");
            //                    }
            //                    return false;
            //                }).WriteTo
            //                .File(LogFilePath("模块目录"), rollingInterval: RollingInterval.Day, outputTemplate: template))
            //                .CreateLogger();
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Fatal)
                .MinimumLevel.Override("Quartz", LogEventLevel.Warning)
                .MinimumLevel.Override("Serilog", LogEventLevel.Information)
                // 全部日志写入到Console
                .WriteTo.Console()
                .WriteTo.Async(c => c.Console(
                   theme: AnsiConsoleTheme.Literate,
                    outputTemplate: template))
                // Debug日志写入到文件
                //.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug))
                //.WriteTo.Async(c => c.File(
                //    path: "Logs/Debug_.txt",
                //    rollingInterval: RollingInterval.Day,
                //    fileSizeLimitBytes: 1024 * 1024 * 10,
                //    retainedFileCountLimit: 100,
                //    outputTemplate: template,
                //    restrictedToMinimumLevel: LogEventLevel.Debug))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Information)
                        .WriteTo.File("Logs/info/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Debug)
                        .WriteTo.File("Logs/debug/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Error)
                        .WriteTo.File("Logs/error/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Verbose)
                        .WriteTo.File("Logs/verbose/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Warning)
                        .WriteTo.File("Logs/warning/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) =>
                        {
                            var result = e.Properties.FirstOrDefault(c => c.Key == "ExternalSystems").Value;
                            if (result != null)
                            {
                                return result.ObjToBool();
                            }
                            return false;
                        })
                        .WriteTo.File("Logs/api/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .CreateLogger();
 
            services.AddSerilog(Log.Logger);
            return services;
        }
    }
}