hwh
2024-06-19 eede0d8477e7117e652b870cb29ef78a95b7f38a
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
using Serilog.Events;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
 
namespace Wms
{
    public static class LogExtends
    {
        //const string infoPath = "Logs/Information.log";
        //const string warnPath = "Logs/Warning.log";
        //const string errorPath = "Logs/Error.log";
        //const string fatalPath = "Logs/Fatal.log";
        //const string template = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
 
        //// 可以将日志输出到控制台、文件、数据库、ES等
        //public static void AddSerilog(this IServiceCollection c)
        //{
        //    Log.Logger = new LoggerConfiguration()
        //        .MinimumLevel.Information()
        //        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // 排除Dotnet自带的日志
        //        .Enrich.FromLogContext()
        //        .WriteTo.Console(outputTemplate: template)
        //        .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Information).WriteTo.Async(congfig => congfig.File(
        //                infoPath,
        //                rollingInterval: RollingInterval.Day,
        //                fileSizeLimitBytes: 1024 * 1024 * 10,    //默认1GB
        //                retainedFileCountLimit: 100,                   //保留最近多少个文件,默认31个
        //                rollOnFileSizeLimit: true,                       //超过文件大小时,自动创建新文件  
        //                shared: true,
        //                outputTemplate: template)
        //            ))
 
        //        .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Warning).WriteTo.Async(congfig => congfig.File(
        //                warnPath,
        //                rollingInterval: RollingInterval.Day,
        //                fileSizeLimitBytes: 1024 * 1024 * 10,
        //                retainedFileCountLimit: 100,
        //                rollOnFileSizeLimit: true,
        //                shared: true,
        //                outputTemplate: template)
        //        ))
 
        //        .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Error).WriteTo.Async(congfig => congfig.File(
        //                errorPath,
        //                rollingInterval: RollingInterval.Day,
        //                fileSizeLimitBytes: 1024 * 1024 * 10,
        //                retainedFileCountLimit: 100,
        //                rollOnFileSizeLimit: true,
        //                shared: true,
        //                outputTemplate: template)
        //        ))
 
        //        .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Fatal).WriteTo.Async(congfig => congfig.File(
        //                fatalPath,
        //                rollingInterval: RollingInterval.Day,
        //                fileSizeLimitBytes: 1024 * 1024 * 10,
        //                retainedFileCountLimit: 100,
        //                rollOnFileSizeLimit: true,
        //                shared: true,
        //                outputTemplate: template)
        //        )).CreateLogger();
 
        //    // 注入到容器
        //    c.AddLogging(opt =>
        //    {
        //        opt.ClearProviders();
        //        opt.AddSerilog(dispose: true);
        //    });
        //}
    }
}