2
0

4 Commits 5848ffa219 ... 04fadd254f

Autor SHA1 Mensagem Data
  Yuli 04fadd254f merge from NLog há 6 dias atrás
  旭 王 2a8b08ad65 新增四个Target将4种不同类型数据显示到UI上 há 6 dias atrás
  旭 王 391726175e 6种权限全部测试完毕 há 6 dias atrás
  旭 王 5d7d1faae8 使用NLog实现了文件分层/异步写入 há 6 dias atrás

+ 2 - 0
SKMC.API/Common/Logger/ILogger.cs

@@ -24,7 +24,9 @@ namespace SKMC.Api.Common.Logger
         void Warn(string message);
 
         void Error(string message, Exception ex = null);
+        void Error(Exception ex,string message);
 
         void Fatal(string message, Exception ex = null);
+        void Fatal(Exception ex,string message);
     }
 }

+ 8 - 1
SKMC.API/Common/Logger/Log4netLogger.cs

@@ -46,10 +46,17 @@ namespace SKMC.Api.Common.Logger
         {
             log.Error(message, ex);
         }
-
+        public void Error(Exception ex, string message)
+        {
+            log.Error(message, ex);
+        }
         public void Fatal(string message, Exception ex = null)
         {
             log.Fatal(message, ex);
         }
+        public void Fatal(Exception ex, string message)
+        {
+            log.Fatal(message,ex);
+        }
     }
 }

+ 5 - 1
SKMC.API/Common/Logger/LogDataService.cs

@@ -14,6 +14,10 @@ namespace SKMC.Api.Common.Logger
 
         private readonly object lockObject = new object();
         public ObservableCollection<LoggingEventModel> LogEventModels { get; set; } = new ObservableCollection<LoggingEventModel>();
+        public ObservableCollection<LoggingEventModel> LogProductionModels { get; set; } = new ObservableCollection<LoggingEventModel>();
+        public ObservableCollection<LoggingEventModel> LogParameterModels { get; set; } = new ObservableCollection<LoggingEventModel>();
+        public ObservableCollection<LoggingEventModel> LogProcessModels { get; set; } = new ObservableCollection<LoggingEventModel>();
+        public ObservableCollection<LoggingEventModel> LogActionModels { get; set; } = new ObservableCollection<LoggingEventModel>();
 
         public static LogDataService Instance()
         {
@@ -23,7 +27,7 @@ namespace SKMC.Api.Common.Logger
 
         private LogDataService()
         {
-            BindingOperations.EnableCollectionSynchronization(LogEventModels, lockObject);
+            //BindingOperations.EnableCollectionSynchronization(LogEventModels, lockObject);
         }
     }
 }

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

@@ -12,7 +12,7 @@ namespace SKMC.Api.Common.Logger
         /// <summary>
         /// "Log4Net" 或 "NLog"
         /// </summary>
-        private static readonly string LoggerType = "Log4Net";
+        private static readonly string LoggerType = "NLog";
 
         public static ILogger Get(Type type)
         {
@@ -32,6 +32,7 @@ namespace SKMC.Api.Common.Logger
 
         public static ILogger Get([CallerFilePath] string callerFilePath = "")
         {
+
             var callerType = System.IO.Path.GetFileNameWithoutExtension(callerFilePath);
             if (LoggerType == "Log4Net")
             {
@@ -46,5 +47,37 @@ namespace SKMC.Api.Common.Logger
                 throw new NotSupportedException($"Logger type {LoggerType} is not supported.");
             }
         }
+
+        public static ILogger Get(LogCategory logCategory)
+        {
+            if (logCategory == LogCategory.ProductionLogger)
+            {
+                return new NLogLogger("ProductionLogger");
+            }
+            else if(logCategory == LogCategory.ParameterLogger)
+            {
+                return new NLogLogger("ParameterLogger");
+            }
+            else if (logCategory == LogCategory.ProcessLogger)
+            {
+                return new NLogLogger("ProcessLogger");
+            }
+            else if(logCategory == LogCategory.ActionLogger)
+            {
+                return new NLogLogger("ActionLogger");
+            }
+            else
+            {
+                throw new NotSupportedException($"Logger category {logCategory.ToString()} is not supported.");
+            }
+        }
+        public enum LogCategory
+        {
+            ProductionLogger,
+            ParameterLogger,
+            ProcessLogger,
+            ActionLogger
+        }
+
     }
 }

+ 30 - 0
SKMC.API/Common/Logger/LoggingActionTarget.cs

@@ -0,0 +1,30 @@
+using NLog;
+using NLog.Targets;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SKMC.Api.Common.Logger
+{
+    /// <summary>
+    /// NLog 扩展
+    /// </summary>
+    public class LoggingActionTarget : TargetWithLayout
+    {
+        static readonly int LOGVIEW_MAXLINE = 1000;
+
+        private readonly LogDataService logDataService = LogDataService.Instance();
+
+        protected override void Write(LogEventInfo loggingEvent)
+        {
+            logDataService.LogActionModels.Insert(0, new LoggingEventModel(loggingEvent));
+
+            if (logDataService.LogActionModels.Count == LOGVIEW_MAXLINE)
+            {
+                logDataService.LogActionModels.RemoveAt(LOGVIEW_MAXLINE - 1);
+            }
+        }
+    }
+}

+ 1 - 1
SKMC.API/Common/Logger/LoggingEventModel.cs

@@ -34,7 +34,7 @@ namespace SKMC.Api.Common.Logger
             Level = loggingEvent.Level.Name;
             Thread = Convert.ToString(System.Threading.Thread.CurrentThread.ManagedThreadId);
             Message = loggingEvent.Message;
-            Exception = loggingEvent.Exception.Message;
+            Exception = loggingEvent.Exception?.Message;
         }
     }
 }

+ 30 - 0
SKMC.API/Common/Logger/LoggingParameterTarget.cs

@@ -0,0 +1,30 @@
+using NLog;
+using NLog.Targets;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SKMC.Api.Common.Logger
+{
+    /// <summary>
+    /// NLog 扩展
+    /// </summary>
+    public class LoggingParameterTarget : TargetWithLayout
+    {
+        static readonly int LOGVIEW_MAXLINE = 1000;
+
+        private readonly LogDataService logDataService = LogDataService.Instance();
+
+        protected override void Write(LogEventInfo loggingEvent)
+        {
+            logDataService.LogParameterModels.Insert(0, new LoggingEventModel(loggingEvent));
+
+            if (logDataService.LogParameterModels.Count == LOGVIEW_MAXLINE)
+            {
+                logDataService.LogParameterModels.RemoveAt(LOGVIEW_MAXLINE - 1);
+            }
+        }
+    }
+}

+ 30 - 0
SKMC.API/Common/Logger/LoggingProcessTarget.cs

@@ -0,0 +1,30 @@
+using NLog;
+using NLog.Targets;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SKMC.Api.Common.Logger
+{
+    /// <summary>
+    /// NLog 扩展
+    /// </summary>
+    public class LoggingProcessTarget : TargetWithLayout
+    {
+        static readonly int LOGVIEW_MAXLINE = 1000;
+
+        private readonly LogDataService logDataService = LogDataService.Instance();
+
+        protected override void Write(LogEventInfo loggingEvent)
+        {
+            logDataService.LogProcessModels.Insert(0, new LoggingEventModel(loggingEvent));
+
+            if (logDataService.LogProcessModels.Count == LOGVIEW_MAXLINE)
+            {
+                logDataService.LogProcessModels.RemoveAt(LOGVIEW_MAXLINE - 1);
+            }
+        }
+    }
+}

+ 30 - 0
SKMC.API/Common/Logger/LoggingProductionTarget.cs

@@ -0,0 +1,30 @@
+using NLog;
+using NLog.Targets;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SKMC.Api.Common.Logger
+{
+    /// <summary>
+    /// NLog 扩展
+    /// </summary>
+    public class LoggingProductionTarget: TargetWithLayout
+    {
+        static readonly int LOGVIEW_MAXLINE = 1000;
+
+        private readonly LogDataService logDataService = LogDataService.Instance();
+
+        protected override void Write(LogEventInfo loggingEvent)
+        {
+            logDataService.LogProductionModels.Insert(0, new LoggingEventModel(loggingEvent));
+
+            if (logDataService.LogProductionModels.Count == LOGVIEW_MAXLINE)
+            {
+                logDataService.LogProductionModels.RemoveAt(LOGVIEW_MAXLINE - 1);
+            }
+        }
+    }
+}

+ 9 - 1
SKMC.API/Common/Logger/NLogLogger.cs

@@ -28,7 +28,7 @@ namespace SKMC.Api.Common.Logger
 
         public void Trace(string message)
         {
-            throw new NotImplementedException();
+            log.Trace(message);
         }
 
         public void Warn(string message)
@@ -40,10 +40,18 @@ namespace SKMC.Api.Common.Logger
         {
             log.Error(message, ex);
         }
+        public void Error(Exception ex,string message)
+        {
+            log.Error(ex, message);
+        }
 
         public void Fatal(string message, Exception ex = null)
         {
             log.Fatal(message, ex);
         }
+        public void Fatal(Exception ex,string message)
+        {
+            log.Fatal(ex, message);
+        }
     }
 }

+ 85 - 0
SKMC.API/Nlog.config

@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
+      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+	<!-- 定义日志输出格式模板 -->
+	<variable name="myLayout" value="[${longdate}] ${level:uppercase=true} [${threadid}][${callsite:className=true:methodName=false:includeNamespace=false:skipFrames=1}] - ${message}${newline}${exception:format=toString}" />
+
+	<!-- 全局设置:定义四个不同的异步文件目标 -->
+	<targets>
+		<!-- 1. 生产日志 (运行生产、机台操作、MES通讯) -->
+		<target name="ProductionFile" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Discard">
+			<target xsi:type="File"
+                    fileName="D:/SkyUnion/SK.RF3.Data/Log/生产/${shortdate}.log"
+                    layout="${myLayout}"
+                    archiveEvery="Day"
+                    archiveAboveSize="104857600"
+                    archiveNumbering="Sequence"
+                    maxArchiveFiles="30"
+                    concurrentWrites="true"
+                    keepFileOpen="false"
+                    encoding="utf-8"/>
+		</target>
+
+		<!-- 2. 参数日志 (参数修改) -->
+		<target name="ParameterFile" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Discard">
+			<target xsi:type="File"
+                    fileName="D:/SkyUnion/SK.RF3.Data/Log/参数/${shortdate}.log"
+                    layout="${myLayout}"
+                    archiveEvery="Day"
+                    archiveAboveSize="104857600"
+                    archiveNumbering="Sequence"
+                    maxArchiveFiles="30"
+                    concurrentWrites="true"
+                    keepFileOpen="false"
+                    encoding="utf-8"/>
+		</target>
+
+		<!-- 3. 流程日志 (流程步骤及状态) -->
+		<target name="ProcessFile" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Discard">
+			<target xsi:type="File"
+                    fileName="D:/SkyUnion/SK.RF3.Data/Log/流程/${shortdate}.log"
+                    layout="${myLayout}"
+                    archiveEvery="Day"
+                    archiveAboveSize="104857600"
+                    archiveNumbering="Sequence"
+                    maxArchiveFiles="30"
+                    concurrentWrites="true"
+                    keepFileOpen="false"
+                    encoding="utf-8"/>
+		</target>
+
+		<!-- 4. 动作日志 (动作执行、视觉通讯) -->
+		<target name="ActionFile" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Discard">
+			<target xsi:type="File"
+                    fileName="D:/SkyUnion/SK.RF3.Data/Log/动作/${shortdate}.log"
+                    layout="${myLayout}"
+                    archiveEvery="Day"
+                    archiveAboveSize="104857600"
+                    archiveNumbering="Sequence"
+                    maxArchiveFiles="30"
+                    concurrentWrites="true"
+                    keepFileOpen="false"
+                    encoding="utf-8"/>
+		</target>
+		<target name="LoggingProductionTarget" xsi:type="LoggingProductionTarget"/>
+		<target name="LoggingParameterTarget" xsi:type="LoggingParameterTarget"/>
+		<target name="LoggingProcessTarget" xsi:type="LoggingProcessTarget"/>
+		<target name="LoggingActionTarget" xsi:type="LoggingActionTarget"/>
+	</targets>
+
+	<!-- 规则设置:将不同名称的 logger 映射到对应的目标文件 -->
+	<rules>
+		<!-- 所有名为 "ProductionLogger" 的日志写入 生产.log -->
+		<logger name="ProductionLogger" minlevel="Trace" writeTo="ProductionFile,LoggingProductionTarget"/>
+
+		<!-- 所有名为 "ParameterLogger" 的日志写入 参数.log -->
+		<logger name="ParameterLogger" minlevel="Trace" writeTo="ParameterFile,LoggingParameterTarget"/>
+
+		<!-- 所有名为 "ProcessLogger" 的日志写入 流程.log -->
+		<logger name="ProcessLogger" minlevel="Trace" writeTo="ProcessFile,LoggingProcessTarget"/>
+
+		<!-- 所有名为 "ActionLogger" 的日志写入 动作.log -->
+		<logger name="ActionLogger" minlevel="Trace" writeTo="ActionFile,LoggingActionTarget"/>
+	</rules>
+</nlog>

+ 19 - 1
SKMC.API/SKMC.API.csproj

@@ -33,6 +33,23 @@
     <WarningLevel>4</WarningLevel>
     <PlatformTarget>x64</PlatformTarget>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+    <DebugSymbols>true</DebugSymbols>
+    <OutputPath>bin\x64\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DebugType>full</DebugType>
+    <PlatformTarget>x64</PlatformTarget>
+    <LangVersion>7.3</LangVersion>
+    <ErrorReport>prompt</ErrorReport>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+    <OutputPath>bin\x64\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <Optimize>true</Optimize>
+    <PlatformTarget>x64</PlatformTarget>
+    <LangVersion>7.3</LangVersion>
+    <ErrorReport>prompt</ErrorReport>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="DryIoc, Version=4.7.7.0, Culture=neutral, PublicKeyToken=dfbf2bd50fcf7768, processorArchitecture=MSIL">
       <HintPath>..\packages\DryIoc.dll.4.7.7\lib\net45\DryIoc.dll</HintPath>
@@ -44,7 +61,7 @@
       <HintPath>..\packages\Microsoft.Xaml.Behaviors.Wpf.1.1.77\lib\net462\Microsoft.Xaml.Behaviors.dll</HintPath>
     </Reference>
     <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
-      <HintPath>H:\SKMC\packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
+      <HintPath>..\packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
     </Reference>
     <Reference Include="NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
       <HintPath>..\packages\NLog.5.3.4\lib\net46\NLog.dll</HintPath>
@@ -220,6 +237,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />
+    <None Include="NLog.Config" />
     <None Include="packages.config" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />