Jiangsheng 3 dagen geleden
bovenliggende
commit
941fc64725
3 gewijzigde bestanden met toevoegingen van 29 en 3 verwijderingen
  1. 10 2
      SKMC.API/Common/Logger/LogDataService.cs
  2. 15 1
      SKMC.API/Common/Logger/LogFactory.cs
  3. 4 0
      SKMC.API/SKMC.API.csproj

+ 10 - 2
SKMC.API/Common/Logger/LogDataService.cs

@@ -12,7 +12,11 @@ namespace SKMC.Api.Common.Logger
     {
         private static LogDataService instance;
 
-        private readonly object lockObject = new object();
+        private readonly object lockEventObject = new object();
+        private readonly object lockProductionObject = new object();
+        private readonly object lockParameterObject = new object();
+        private readonly object lockProcessObject = new object();
+        private readonly object lockActionObject = new object();
         public ObservableCollection<LoggingEventModel> LogEventModels { get; set; } = new ObservableCollection<LoggingEventModel>();
 
         public static LogDataService Instance()
@@ -23,7 +27,11 @@ namespace SKMC.Api.Common.Logger
 
         private LogDataService()
         {
-            BindingOperations.EnableCollectionSynchronization(LogEventModels, lockObject);
+            BindingOperations.EnableCollectionSynchronization(LogEventModels, lockEventObject);
+            BindingOperations.EnableCollectionSynchronization(LogProductionModels, lockProductionObject);
+            BindingOperations.EnableCollectionSynchronization(LogParameterModels, lockParameterObject);
+            BindingOperations.EnableCollectionSynchronization(LogProcessModels, lockProcessObject);
+            BindingOperations.EnableCollectionSynchronization(LogActionModels, lockActionObject);
         }
     }
 }

+ 15 - 1
SKMC.API/Common/Logger/LogFactory.cs

@@ -1,4 +1,5 @@
-using System;
+using NLog;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.CompilerServices;
@@ -9,6 +10,19 @@ namespace SKMC.Api.Common.Logger
 {
     public class LogFactory
     {
+        static LogFactory()
+        {
+            // NLog特殊处理,需要先注册Target再获取LOG实例,否则Target内部方法无法触发。
+            NLog.LogManager.Setup()
+                .SetupExtensions(ext =>
+                {
+                    ext.RegisterTarget<LoggingProductionTarget>("LoggingProductionTarget");
+                    ext.RegisterTarget<LoggingParameterTarget>("LoggingParameterTarget");
+                    ext.RegisterTarget<LoggingProcessTarget>("LoggingProcessTarget");
+                    ext.RegisterTarget<LoggingActionTarget>("LoggingActionTarget");
+                })
+                .LoadConfigurationFromFile("nlog.config");
+        }
         /// <summary>
         /// "Log4Net" 或 "NLog"
         /// </summary>

+ 4 - 0
SKMC.API/SKMC.API.csproj

@@ -86,6 +86,10 @@
     <Compile Include="Client\Config\ClientConstants.cs" />
     <Compile Include="Client\Config\ClientException.cs" />
     <Compile Include="Client\Views\BaseEditorModel.cs" />
+    <Compile Include="Common\Logger\LoggingActionTarget.cs" />
+    <Compile Include="Common\Logger\LoggingParameterTarget.cs" />
+    <Compile Include="Common\Logger\LoggingProcessTarget.cs" />
+    <Compile Include="Common\Logger\LoggingProductionTarget.cs" />
     <Compile Include="Core\Exceptions\ExceptionCacher.cs" />
     <Compile Include="Common\DB\IModelConverter.cs" />
     <Compile Include="Client\Access\ClientAccess.cs" />