From b67097ff296b7fa29961b11cd537c0bb0bb3ecc8 Mon Sep 17 00:00:00 2001
From: 王 垚 <1402714037@qq.com>
Date: 星期一, 11 四月 2022 14:31:18 +0800
Subject: [PATCH] 日志文件

---
 WebTM/App_Data/LogService.cs |   71 +++++++++++++++++++++++++++++++++++
 WebTM/WebTM.csproj           |    5 ++
 2 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/WebTM/App_Data/LogService.cs b/WebTM/App_Data/LogService.cs
new file mode 100644
index 0000000..90a7c0b
--- /dev/null
+++ b/WebTM/App_Data/LogService.cs
@@ -0,0 +1,71 @@
+锘縰sing Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+public class LogService
+{
+    private static readonly object lockObj = new object();
+
+    public static void WriteAsync(object obj, string filePath = "Vlog", bool isAppend = true)
+    {
+        Task.Run(() =>
+        {
+            Write(obj, filePath, isAppend);
+        });
+    }
+
+    public static void WriteAsync<T>(object obj, string filePath = "Vlog", bool isAppend = true)
+    {
+        Task.Run(() =>
+        {
+            Write(obj, $@"{filePath}\{typeof(T).Name}", isAppend);
+        });
+    }
+
+    #region 鏃ュ織
+    public static void Write(object obj, string filePath = "Vlog", bool isAppend = true)
+    {
+        try
+        {
+            lock (lockObj)
+            {
+                filePath = $@"{filePath}\webapi{DateTime.Now.ToString("yyyyMMdd")}.txt";
+
+                filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
+
+                if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
+                {
+                    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
+                }
+
+                bool fileExists = System.IO.File.Exists(filePath);
+                //涓嶅瓨鍦� 鍒欏垱寤鸿鏂囦欢
+                if (!fileExists)
+                {
+                    System.IO.File.Create(filePath).Close();
+                }
+
+                using (StreamWriter writer = new StreamWriter(filePath, isAppend))
+                {
+                    //瀛樺湪鐨勬椂鍊欐墠鍐欎竴琛�
+                    if (fileExists && isAppend)
+                    {
+                        writer.WriteLine();
+                    }
+
+                    var content = obj is string ? obj : JsonConvert.SerializeObject(obj);
+                    writer.WriteLine($"{DateTime.Now} {content}");
+                }
+            }
+        }
+        catch (Exception ex)
+        {
+        }
+    }
+    #endregion
+}
+
diff --git a/WebTM/WebTM.csproj b/WebTM/WebTM.csproj
index 22cc0a0..2066939 100644
--- a/WebTM/WebTM.csproj
+++ b/WebTM/WebTM.csproj
@@ -41,6 +41,10 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="Microsoft.CSharp" />
+    <Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\..\..\鍔炲叕\K3Cloud\WebSite\bin\Newtonsoft.Json.dll</HintPath>
+    </Reference>
     <Reference Include="System.Web.DynamicData" />
     <Reference Include="System.Web.Entity" />
     <Reference Include="System.Web.ApplicationServices" />
@@ -1039,6 +1043,7 @@
     <None Include="Properties\PublishProfiles\Web.pubxml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="App_Data\LogService.cs" />
     <Compile Include="App_Data\MssqlReportData.cs" />
     <Compile Include="App_Data\ReportData.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

--
Gitblit v1.9.1