Blame view

netcore/src/Infrastructure/NCC/Logging/Internal/StringLoggingPartMethods.cs 2.46 KB
de2bd2f9   “wangming”   项目初始化
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
  using Microsoft.Extensions.Logging;
  
  namespace NCC.Logging
  {
      /// <summary>
      /// 构建字符串日志部分类
      /// </summary>
      public sealed partial class StringLoggingPart
      {
          /// <summary>
          /// Information
          /// </summary>
          public void LogInformation()
          {
              SetLevel(LogLevel.Information).Log();
          }
  
          /// <summary>
          /// Warning
          /// </summary>
          public void LogWarning()
          {
              SetLevel(LogLevel.Warning).Log();
          }
  
          /// <summary>
          /// Error
          /// </summary>
          public void LogError()
          {
              SetLevel(LogLevel.Error).Log();
          }
  
          /// <summary>
          /// Debug
          /// </summary>
          public void LogDebug()
          {
              SetLevel(LogLevel.Debug).Log();
          }
  
          /// <summary>
          /// Trace
          /// </summary>
          public void LogTrace()
          {
              SetLevel(LogLevel.Trace).Log();
          }
  
          /// <summary>
          /// Critical
          /// </summary>
          public void LogCritical()
          {
              SetLevel(LogLevel.Critical).Log();
          }
  
          /// <summary>
          /// 写入日志
          /// </summary>
          /// <returns></returns>
          public void Log()
          {
              if (Message == null) return;
  
              var logger = !string.IsNullOrWhiteSpace(CategoryName)
                 ? App.GetService<ILoggerFactory>(LoggerScoped ?? App.RootServices)?.CreateLogger(CategoryName)
                 : App.GetService(typeof(ILogger<>).MakeGenericType(CategoryType), LoggerScoped ?? App.RootServices) as ILogger;
  
              // 如果没有异常且事件 Id 为空
              if (Exception == null && EventId == null)
              {
                  logger.Log(Level, Message, Args);
              }
              // 如果存在异常且事件 Id 为空
              else if (Exception != null && EventId == null)
              {
                  logger.Log(Level, Exception, Message, Args);
              }
              // 如果异常为空且事件 Id 不为空
              else if (Exception == null && EventId != null)
              {
                  logger.Log(Level, EventId.Value, Message, Args);
              }
              // 如果存在异常且事件 Id 不为空
              else if (Exception != null && EventId != null)
              {
                  logger.Log(Level, EventId.Value, Exception, Message, Args);
              }
              else { }
          }
      }
  }