.net6 ILogger日志保存到本地

1、新建一个LocalFileLogger的类

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
public class LocalFileLogger : ILogger
   {
       private readonly string categoryName;
       private readonly string basePath;
 
       public LocalFileLogger(string categoryName)
       {
           this.categoryName = categoryName;
           string[] fieldstrs = Enum.GetNames(typeof(LogLevel));
           foreach (var item in fieldstrs)
           {
               basePath = Directory.GetCurrentDirectory().Replace("\\", "/") + "/Logs/"+$"/{item}/";
 
               if (Directory.Exists(basePath) == false)
               {
                   Directory.CreateDirectory(basePath);
               }
           }
           basePath = Directory.GetCurrentDirectory().Replace("\\", "/") + "/Logs/";
       }
 
       public IDisposable BeginScope<TState>(TState state)
       {
           return default!;
       }
 
       public bool IsEnabled(LogLevel logLevel)
       {
           if (logLevel != LogLevel.None)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
 
       public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
       {
           if (IsEnabled(logLevel))
           {
               if (state != null && state.ToString() != null)
               {
                   var logContent = state.ToString();
 
                   if (logContent != null)
                   {
                       if (exception != null)
                       {
                           var logMsg = new
                           {
                               message = logContent,
                               error = new
                               {
                                   exception?.Source,
                                   exception?.Message,
                                   exception?.StackTrace
                               }
                           };
 
                           logContent = System.Text.Json.JsonSerializer.Serialize(logMsg);
                       }
 
                       var log = new
                       {
                           CreateTime = DateTime.UtcNow,
                           Category = categoryName,
                           Level = logLevel.ToString(),
                           Content = logContent
                       };
 
                       string logStr = System.Text.Json.JsonSerializer.Serialize(log);
 
                       string logPath=string.Empty;
                       switch (logLevel)
                       {
                           case LogLevel.Trace:
                            logPath= basePath + "/Trace/"+ DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.Debug:
                               logPath = basePath + "/Debug/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.Information:
                               logPath = basePath + "/Information/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.Warning:
                               logPath = basePath + "/Warning/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.Error:
                               logPath = basePath + "/Error/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.Critical:
                               logPath = basePath + "/Critical/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           case LogLevel.None:
                               logPath = basePath + "/None/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                           default:
                               logPath = basePath + "/Start/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                               break;
                       }
                       File.AppendAllTextAsync(logPath, logStr + Environment.NewLine, System.Text.Encoding.UTF8);
                       
                   }
                   
                   
               }
           }
       }
   }

2、新建LocalFileLoggerProvider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class LocalFileLoggerProvider : ILoggerProvider
{
    private readonly System.Collections.Concurrent.ConcurrentDictionary<string, LocalFileLogger> loggers = new();
 
    public ILogger CreateLogger(string categoryName)
    {
        return loggers.GetOrAdd(categoryName, new LocalFileLogger(categoryName));
    }
 
    public void Dispose()
    {
        loggers.Clear();
        GC.SuppressFinalize(this);
    }
}

  最后在Program注册服务

 

posted @   ¥东方不败  阅读(166)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示