Browse Source

移除BlackBox包,将程序状态日志移入Core库中,并修改了原Log的namespace

yuzhengyang 7 years ago
parent
commit
5ba35c7c49
27 changed files with 379 additions and 383 deletions
  1. BIN
      Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide
  2. BIN
      Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide-shm
  3. BIN
      Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide-wal
  4. BIN
      Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide
  5. BIN
      Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-shm
  6. BIN
      Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-wal
  7. 0 23
      Fork.Net/Azylee.Utils/Azylee.BlackBox/App.config
  8. 0 66
      Fork.Net/Azylee.Utils/Azylee.BlackBox/Azylee.BlackBox.csproj
  9. 0 46
      Fork.Net/Azylee.Utils/Azylee.BlackBox/Models/ExceptionLogs.cs
  10. 0 54
      Fork.Net/Azylee.Utils/Azylee.BlackBox/Models/RunningStatus.cs
  11. 0 36
      Fork.Net/Azylee.Utils/Azylee.BlackBox/Properties/AssemblyInfo.cs
  12. 0 108
      Fork.Net/Azylee.Utils/Azylee.BlackBox/Utils/BlackBoxTool.cs
  13. 4 11
      Fork.Net/Azylee.Utils/Azylee.Core/AppUtils/AppInfoTool.cs
  14. 8 4
      Fork.Net/Azylee.Utils/Azylee.Core/Azylee.Core.csproj
  15. 63 0
      Fork.Net/Azylee.Utils/Azylee.Core/DataUtils/SerializeUtils/SerializeTool.cs
  16. 44 0
      Fork.Net/Azylee.Utils/Azylee.Core/IOUtils/BinaryUtils/BinaryFileTool.cs
  17. 1 1
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/Log.cs
  18. 1 1
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogLevel.cs
  19. 1 1
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogModel.cs
  20. 1 1
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogType.cs
  21. 163 0
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLog.cs
  22. 83 0
      Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLogModel.cs
  23. 3 6
      Fork.Net/Azylee.Utils/Azylee.Core/WindowsUtils/InfoUtils/ComputerStatusTool.cs
  24. 0 11
      Fork.Net/Fork.Net.sln
  25. 1 0
      Fork.Net/Test/Test.BlackBox/Form1.Designer.cs
  26. 6 10
      Fork.Net/Test/Test.BlackBox/Form1.cs
  27. 0 4
      Fork.Net/Test/Test.BlackBox/Test.BlackBox.csproj

BIN
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide


BIN
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide-shm


BIN
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide-wal


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-shm


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-wal


+ 0 - 23
Fork.Net/Azylee.Utils/Azylee.BlackBox/App.config

@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<configuration>
-  <configSections>
-    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
-    
-  </configSections>
-  <connectionStrings>
-    <add name="DefaultConnection" connectionString="data source=|DataDirectory|\BlackBox.db" providerName="System.Data.SQLite" />
-  </connectionStrings>
-  <entityFramework>
-    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
-      <parameters>
-        <parameter value="v13.0" />
-      </parameters>
-    </defaultConnectionFactory>
-    <providers>
-      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
-      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
-      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
-    </providers>
-  </entityFramework>
-  
-</configuration>

+ 0 - 66
Fork.Net/Azylee.Utils/Azylee.BlackBox/Azylee.BlackBox.csproj

@@ -1,66 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}</ProjectGuid>
-    <OutputType>Library</OutputType>
-    <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>Azylee.BlackBox</RootNamespace>
-    <AssemblyName>Azylee.BlackBox</AssemblyName>
-    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
-    <FileAlignment>512</FileAlignment>
-    <NuGetPackageImportStamp>
-    </NuGetPackageImportStamp>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>none</DebugType>
-    <Optimize>true</Optimize>
-    <OutputPath>bin\Release\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-    <Reference Include="System.ComponentModel.DataAnnotations" />
-    <Reference Include="System.Core" />
-    <Reference Include="System.Drawing" />
-    <Reference Include="System.Windows.Forms" />
-    <Reference Include="System.Xml.Linq" />
-    <Reference Include="System.Data.DataSetExtensions" />
-    <Reference Include="Microsoft.CSharp" />
-    <Reference Include="System.Data" />
-    <Reference Include="System.Xml" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="Models\ExceptionLogs.cs" />
-    <Compile Include="Models\RunningStatus.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="Utils\BlackBoxTool.cs" />
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="App.config" />
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\Azylee.Core.Plus\Azylee.Core.Plus.csproj">
-      <Project>{915ae524-7efd-4ecc-b731-de1d1f5558f0}</Project>
-      <Name>Azylee.Core.Plus</Name>
-    </ProjectReference>
-    <ProjectReference Include="..\Azylee.Core\Azylee.Core.csproj">
-      <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
-      <Name>Azylee.Core</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-</Project>

+ 0 - 46
Fork.Net/Azylee.Utils/Azylee.BlackBox/Models/ExceptionLogs.cs

@@ -1,46 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Azylee.BlackBox.Models
-{
-    /// <summary>
-    /// 异常日志
-    /// </summary>
-    public class ExceptionLogs
-    {
-        /// <summary>
-        /// 主键
-        /// </summary>
-        public Guid Id { get; set; }
-        /// <summary>
-        /// 关键字
-        /// </summary>
-        public string Keywords { get; set; }
-        /// <summary>
-        /// 作者
-        /// </summary>
-        public string Author { get; set; }
-        /// <summary>
-        /// 创建时间
-        /// </summary>
-        public DateTime CreateTime { get; set; }
-        /// <summary>
-        /// 异常名称
-        /// </summary>
-        public string ExceptionName { get; set; }
-        /// <summary>
-        /// 异常消息
-        /// </summary>
-        public string ExceptionMessage { get; set; }
-        /// <summary>
-        /// 异常跟踪
-        /// </summary>
-        public string ExceptionStackTrace { get; set; }
-        /// <summary>
-        /// 备注
-        /// </summary>
-        public string Remark { get; set; }
-    }
-}

+ 0 - 54
Fork.Net/Azylee.Utils/Azylee.BlackBox/Models/RunningStatus.cs

@@ -1,54 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Azylee.BlackBox.Models
-{
-    /// <summary>
-    /// 运行状态
-    /// </summary>
-    public class RunningStatus
-    {
-        /// <summary>
-        /// 时间
-        /// </summary>
-        public DateTime Time { get; set; }
-        /// <summary>
-        /// 统计时长
-        /// </summary>
-        public int Long { get; set; }
-        /// <summary>
-        /// 脱机时长
-        /// </summary>
-        public long AFK { get; set; }
-        /// <summary>
-        /// Cpu使用率
-        /// </summary>
-        public double CpuPer { get; set; }
-        /// <summary>
-        /// 内存容量
-        /// </summary>
-        public long RamSize { get; set; }
-        /// <summary>
-        /// 可用内存
-        /// </summary>
-        public long RamFree { get; set; }
-        /// <summary>
-        /// 系统盘容量
-        /// </summary>
-        public long SysDriveSize { get; set; }
-        /// <summary>
-        /// 可用系统盘容量
-        /// </summary>
-        public long SysDriveFree { get; set; }
-        /// <summary>
-        /// 应用程序Cpu使用率
-        /// </summary>
-        public double AppCpuPer { get; set; }
-        /// <summary>
-        /// 应用程序占用内存
-        /// </summary>
-        public double AppRamUsed { get; set; }
-    }
-}

+ 0 - 36
Fork.Net/Azylee.Utils/Azylee.BlackBox/Properties/AssemblyInfo.cs

@@ -1,36 +0,0 @@
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-// 有关程序集的一般信息由以下
-// 控制。更改这些特性值可修改
-// 与程序集关联的信息。
-[assembly: AssemblyTitle("Azylee.BlackBox")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("Azylee.BlackBox")]
-[assembly: AssemblyCopyright("Copyright ©  2018")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// 将 ComVisible 设置为 false 会使此程序集中的类型
-//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
-//请将此类型的 ComVisible 特性设置为 true。
-[assembly: ComVisible(false)]
-
-// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
-[assembly: Guid("6b84ab76-94b9-4dae-b168-a6e216dd54f0")]
-
-// 程序集的版本信息由下列四个值组成: 
-//
-//      主版本
-//      次版本
-//      生成号
-//      修订号
-//
-// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
-//通过使用 "*",如下所示:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]

+ 0 - 108
Fork.Net/Azylee.Utils/Azylee.BlackBox/Utils/BlackBoxTool.cs

@@ -1,108 +0,0 @@
-using Azylee.BlackBox.Models;
-using Azylee.Core.AppUtils;
-using Azylee.Core.IOUtils.DirUtils;
-using Azylee.Core.IOUtils.TxtUtils;
-using Azylee.Core.Plus.DataUtils.JsonUtils;
-using Azylee.Core.WindowsUtils.APIUtils;
-using Azylee.Core.WindowsUtils.InfoUtils;
-using System;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace Azylee.BlackBox.Utils
-{
-    public sealed class BlackBoxTool
-    {
-        #region 单例模式
-        private static BlackBoxTool _BlackBoxTool;
-        private static readonly object syncObject = new object();
-        private BlackBoxTool() { }
-        public static BlackBoxTool Instance
-        {
-            get
-            {
-                if (_BlackBoxTool == null)
-                    lock (syncObject)
-                        if (_BlackBoxTool == null)
-                            _BlackBoxTool = new BlackBoxTool();
-                return _BlackBoxTool;
-            }
-        }
-        #endregion
-        DateTime Time = DateTime.Now;
-        int Interval = 1 * 1000;
-        Task Listener = null;
-        CancellationTokenSource CancelToken = new CancellationTokenSource();
-        public bool Start()
-        {
-            //如果任务停止运行,则重新创建Token,并释放上次任务
-            if (Listener != null && Listener.Status != TaskStatus.Running)
-            {
-                try
-                {
-                    CancelToken = new CancellationTokenSource();
-                    Listener.Dispose();
-                }
-                catch { }
-            }
-            //如果任务没取消,并且没有运行任务,则运行任务
-            if (!CancelToken.IsCancellationRequested && (Listener == null || Listener.Status != TaskStatus.Running))
-            {
-                Listener = Task.Factory.StartNew(() =>
-                {
-                    try
-                    {
-                        //using (Muse db = new Muse())
-                        //{
-                        //    CollectData(db);
-                        while (!CancelToken.IsCancellationRequested)
-                        {
-                            Time = DateTime.Now;
-                            Thread.Sleep(Interval);
-                            CollectData();
-                        }
-                        //}
-                    }
-                    catch { }
-                }, CancelToken.Token);
-                return true;
-            }
-            return false;
-        }
-        public bool Stop()
-        {
-            try
-            {
-                if (!CancelToken.IsCancellationRequested)
-                {
-                    CancelToken.Cancel();
-                }
-                return true;
-            }
-            catch { return false; }
-        }
-        private void CollectData()
-        {
-            try
-            {
-                RunningStatus status = new RunningStatus()
-                {
-                    Time = Time,
-                    Long = Interval,
-                    AFK = WindowsAPI.GetLastInputTime(),
-                    CpuPer = ComputerStatusTool.CpuUtilization(),
-                    RamSize = (long)ComputerInfoTool.TotalPhysicalMemory(),
-                    RamFree = (long)ComputerInfoTool.AvailablePhysicalMemory(),
-                    SysDriveSize = ComputerInfoTool.GetSystemDriveTotalSize(),
-                    SysDriveFree = ComputerInfoTool.GetSystemDriveAvailableSize(),
-                    AppCpuPer = AppInfoTool.CPU(),
-                    AppRamUsed = AppInfoTool.RAM(),
-                };
-                TxtTool.Append(
-                    DirTool.Combine(AppDomain.CurrentDomain.BaseDirectory, "BlackBox-Status.txt"),
-                    JsonTool.ToStr(status));
-            }
-            catch { }
-        }
-    }
-}

+ 4 - 11
Fork.Net/Azylee.Utils/Azylee.Core/AppUtils/AppInfoTool.cs

@@ -7,29 +7,22 @@ using System.Text;
 namespace Azylee.Core.AppUtils
 {
     public class AppInfoTool
-    {
+    { 
         /// <summary>
-        /// 读取APP占用CPU
+        /// 读取APP Processor(可读取App的CPU使用率)
         /// </summary>
         /// <returns></returns>
-        public static double CPU()
+        public static PerformanceCounter Processor()
         {
-            double value = 0;
             Process p = null;
             PerformanceCounter processor = null;
             try
             {
                 p = Process.GetCurrentProcess();
                 processor = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
-                value = processor.NextValue();
             }
             catch { }
-            finally
-            {
-                processor?.Dispose();
-                p?.Dispose();
-            }
-            return value;
+            return processor;
         }
         /// <summary>
         /// 读取APP占用内存

+ 8 - 4
Fork.Net/Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -58,10 +58,12 @@
     <Compile Include="DataUtils\EncryptUtils\DesTool.cs" />
     <Compile Include="DataUtils\EncryptUtils\MD5Tool.cs" />
     <Compile Include="DataUtils\GuidUtils\GuidTool.cs" />
+    <Compile Include="DataUtils\SerializeUtils\SerializeTool.cs" />
     <Compile Include="DataUtils\StringUtils\StringTool.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressDelegate.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressEventArgs.cs" />
+    <Compile Include="IOUtils\BinaryUtils\BinaryFileTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileCodeTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileCompressTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileEncryptTool.cs" />
@@ -73,10 +75,12 @@
     <Compile Include="IOUtils\TxtUtils\IniTool.cs" />
     <Compile Include="IOUtils\TxtUtils\TxtTool.cs" />
     <Compile Include="IOUtils\TxtUtils\XmlTool.cs" />
-    <Compile Include="LogUtils\Log.cs" />
-    <Compile Include="LogUtils\LogLevel.cs" />
-    <Compile Include="LogUtils\LogModel.cs" />
-    <Compile Include="LogUtils\LogType.cs" />
+    <Compile Include="LogUtils\SimpleLogUtils\Log.cs" />
+    <Compile Include="LogUtils\SimpleLogUtils\LogLevel.cs" />
+    <Compile Include="LogUtils\SimpleLogUtils\LogModel.cs" />
+    <Compile Include="LogUtils\SimpleLogUtils\LogType.cs" />
+    <Compile Include="LogUtils\StatusLogUtils\StatusLogModel.cs" />
+    <Compile Include="LogUtils\StatusLogUtils\StatusLog.cs" />
     <Compile Include="ProcessUtils\ProcessTool.cs" />
     <Compile Include="IOUtils\DirUtils\DirTool.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 63 - 0
Fork.Net/Azylee.Utils/Azylee.Core/DataUtils/SerializeUtils/SerializeTool.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.SerializeUtils
+{
+    public static class SerializeTool
+    {
+        /// <summary>
+        /// 序列化模型到 byte 数组 [Serializable]
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="model"></param>
+        /// <returns></returns>
+        public static byte[] Serialize<T>(T model)
+        {
+            if (model != null)
+            {
+                MemoryStream ms = null;
+                try
+                {
+                    ms = new MemoryStream(); //内存实例
+                    BinaryFormatter formatter = new BinaryFormatter(); //创建序列化的实例
+                    formatter.Serialize(ms, model);//序列化对象,写入ms流中    
+                    byte[] bytes = ms.GetBuffer();
+                    return bytes;
+                }
+                catch { }
+                finally
+                {
+                    ms?.Close();
+                }
+            }
+            return null;
+        }
+        public static T Deserialize<T>(byte[] bytes)
+        {
+            if (bytes != null)
+            {
+                MemoryStream ms = null;
+                try
+                {
+                    object obj = null;
+                    ms = new MemoryStream(bytes); //利用传来的byte[]创建一个内存流
+                    ms.Position = 0;
+                    BinaryFormatter formatter = new BinaryFormatter();
+                    obj = formatter.Deserialize(ms);//把内存流反序列成对象
+                    return (T)Convert.ChangeType(obj, typeof(T)); ;
+                }
+                catch { }
+                finally
+                {
+                    ms?.Close();
+                }
+            }
+            return default(T);
+        }
+    }
+}

+ 44 - 0
Fork.Net/Azylee.Utils/Azylee.Core/IOUtils/BinaryUtils/BinaryFileTool.cs

@@ -0,0 +1,44 @@
+using Azylee.Core.IOUtils.DirUtils;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.IOUtils.BinaryUtils
+{
+    public static class BinaryFileTool
+    {
+        public static bool write(string file, byte[] bytes)
+        {
+            try
+            {
+                DirTool.Create(Path.GetDirectoryName(file));
+                //创建一个文件流
+                using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
+                {
+                    //将byte数组写入文件中
+                    fs.Write(bytes, 0, bytes.Length);
+                }
+                return true;
+            }
+            catch { }
+            return false;
+        }
+        public static byte[] read(string file)
+        {
+            try
+            {
+                using (FileStream fs = new FileStream(file, FileMode.Open))
+                {
+                    long size = fs.Length; //获取文件大小
+                    byte[] array = new byte[size];
+                    fs.Read(array, 0, array.Length); //将文件读到byte数组中
+                    return array;
+                }
+            }
+            catch { }
+            return null;
+        }
+    }
+}

+ 1 - 1
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/Log.cs

@@ -27,7 +27,7 @@ using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
 
-namespace Azylee.Core.LogUtils
+namespace Azylee.Core.LogUtils.SimpleLogUtils
 {
     /// <summary>
     /// Log 输出工具

+ 1 - 1
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogLevel.cs

@@ -4,7 +4,7 @@
 //############################################################
 using System;
 
-namespace Azylee.Core.LogUtils
+namespace Azylee.Core.LogUtils.SimpleLogUtils
 {
     [Flags]
     public enum LogLevel

+ 1 - 1
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogModel.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
-namespace Azylee.Core.LogUtils
+namespace Azylee.Core.LogUtils.SimpleLogUtils
 {
     public  class LogModel
     {

+ 1 - 1
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/LogType.cs

@@ -2,7 +2,7 @@
 //      https://github.com/yuzhengyang
 //      author:yuzhengyang
 //############################################################
-namespace Azylee.Core.LogUtils
+namespace Azylee.Core.LogUtils.SimpleLogUtils
 {
     public enum LogType
     {

+ 163 - 0
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLog.cs

@@ -0,0 +1,163 @@
+//************************************************************************
+//      https://github.com/yuzhengyang
+//      author:     yuzhengyang
+//      date:       2018.4.17 - 2018.4.17
+//      desc:       程序运行状态日志
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
+using Azylee.Core.AppUtils;
+using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.IOUtils.DirUtils;
+using Azylee.Core.IOUtils.FileUtils;
+using Azylee.Core.IOUtils.TxtUtils;
+using Azylee.Core.WindowsUtils.APIUtils;
+using Azylee.Core.WindowsUtils.InfoUtils;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Azylee.Core.LogUtils.StatusLogUtils
+{
+    public sealed class StatusLog
+    {
+        #region 单例模式
+        private static StatusLog _StatusLog;
+        private static readonly object syncObject = new object();
+        private StatusLog() { }
+        public static StatusLog Instance
+        {
+            get
+            {
+                if (_StatusLog == null)
+                    lock (syncObject)
+                        if (_StatusLog == null)
+                            _StatusLog = new StatusLog();
+                return _StatusLog;
+            }
+        }
+        #endregion
+
+        #region 基础属性
+        const string LOG_PATH = "StatusLog";//存储路径
+        const int CACHE_DAYS = 30;//缓存天数
+
+        public string LogPath = LOG_PATH;//存储路径
+
+        DateTime Time = DateTime.Now;//标记当前时间
+        int Interval = 60 * 1000;//监测间隔时间
+
+        Task Listener = null;//监测任务
+        CancellationTokenSource CancelToken = new CancellationTokenSource();//监测取消Token
+        PerformanceCounter ComputerProcessor = ComputerStatusTool.Processor();//电脑CPU监控
+        PerformanceCounter AppProcessor = AppInfoTool.Processor();//程序CPU监控
+        #endregion
+
+        public bool Start()
+        {
+            //如果任务停止运行,则重新创建Token,并释放上次任务
+            if (Listener != null && Listener.Status != TaskStatus.Running)
+            {
+                try
+                {
+                    CancelToken = new CancellationTokenSource();
+                    Listener.Dispose();
+                }
+                catch { }
+            }
+            //如果任务没取消,并且没有运行任务,则运行任务
+            if (!CancelToken.IsCancellationRequested && (Listener == null || Listener.Status != TaskStatus.Running))
+            {
+                Listener = Task.Factory.StartNew(() =>
+                {
+                    try
+                    {
+                        //using (Muse db = new Muse())
+                        //{
+                        //    CollectData(db);
+                        while (!CancelToken.IsCancellationRequested)
+                        {
+                            Time = DateTime.Now;
+                            Thread.Sleep(Interval);
+                            CollectData();
+                        }
+                        //}
+                    }
+                    catch { }
+                }, CancelToken.Token);
+                return true;
+            }
+            return false;
+        }
+        public bool Stop()
+        {
+            try
+            {
+                if (!CancelToken.IsCancellationRequested)
+                {
+                    CancelToken.Cancel();
+                }
+                return true;
+            }
+            catch { return false; }
+        }
+        private void CollectData()
+        {
+            try
+            {
+                StatusLogModel status = new StatusLogModel()
+                {
+                    Time = Time,
+                    Long = Interval,
+                    AFK = WindowsAPI.GetLastInputTime(),
+                    CpuPer = (int)ComputerProcessor.NextValue(),
+                    RamSize = (long)ComputerInfoTool.TotalPhysicalMemory(),
+                    RamFree = (long)ComputerInfoTool.AvailablePhysicalMemory(),
+                    SysDriveSize = ComputerInfoTool.GetSystemDriveTotalSize(),
+                    SysDriveFree = ComputerInfoTool.GetSystemDriveAvailableSize(),
+                    AppCpuPer = (int)AppProcessor.NextValue(),
+                    AppRamUsed = AppInfoTool.RAM(),
+                };
+
+                WriteFile(status);
+                Cleaner();
+            }
+            catch { }
+        }
+        private void WriteFile(StatusLogModel status)
+        {
+            //设置日志目录
+            string logPath = AppDomain.CurrentDomain.BaseDirectory + LogPath;
+            string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
+            //创建日志目录
+            DirTool.Create(logPath);
+            //写出日志
+            TxtTool.Append(file, status.ToString());
+        }
+        private void Cleaner()
+        {
+            List<string> files = FileTool.GetFile(AppDomain.CurrentDomain.BaseDirectory + LogPath);
+            if (ListTool.HasElements(files))
+            {
+                files.ForEach(f =>
+                {
+                    try
+                    {
+                        string filename = Path.GetFileNameWithoutExtension(f);
+                        if (filename.Length == 10)
+                        {
+                            DateTime date = DateTime.Parse(filename);
+                            if (date < Time.AddDays(-CACHE_DAYS - 1)) FileTool.Delete(f);
+                        }
+                        else { FileTool.Delete(f); }
+                    }
+                    catch { FileTool.Delete(f); }
+                });
+            }
+        }
+    }
+}

+ 83 - 0
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLogModel.cs

@@ -0,0 +1,83 @@
+using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.DataUtils.DateTimeUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.LogUtils.StatusLogUtils
+{
+    /// <summary>
+    /// 运行状态
+    /// </summary>
+    public class StatusLogModel
+    {
+        /// <summary>
+        /// 时间
+        /// </summary>
+        public DateTime Time { get; set; }
+        /// <summary>
+        /// 统计时长
+        /// </summary>
+        public int Long { get; set; }
+        /// <summary>
+        /// 脱机时长
+        /// </summary>
+        public long AFK { get; set; }
+        /// <summary>
+        /// Cpu使用率
+        /// </summary>
+        public int CpuPer { get; set; }
+        /// <summary>
+        /// 内存容量
+        /// </summary>
+        public long RamSize { get; set; }
+        /// <summary>
+        /// 可用内存
+        /// </summary>
+        public long RamFree { get; set; }
+        /// <summary>
+        /// 系统盘容量
+        /// </summary>
+        public long SysDriveSize { get; set; }
+        /// <summary>
+        /// 可用系统盘容量
+        /// </summary>
+        public long SysDriveFree { get; set; }
+        /// <summary>
+        /// 应用程序Cpu使用率
+        /// </summary>
+        public int AppCpuPer { get; set; }
+        /// <summary>
+        /// 应用程序占用内存
+        /// </summary>
+        public double AppRamUsed { get; set; }
+
+        public override string ToString()
+        {
+            string s = $"{DateTimeConvert.StandardString(Time)}|{Long}|{AFK}|{CpuPer}|" +
+                $"{RamSize}|{RamFree}|{SysDriveSize}|{SysDriveFree}|" +
+                $"{AppCpuPer}|{AppRamUsed}";
+            return s;
+        }
+        public StatusLogModel FromString(string s)
+        {
+            StatusLogModel model = new StatusLogModel();
+            string[] elements = s.Split('|');
+            if (ListTool.HasElements(elements))
+            {
+                try { if (elements.Length > 0) model.Time = DateTime.Parse(elements[0]); } catch { }
+                try { if (elements.Length > 1) model.Long = int.Parse(elements[1]); } catch { }
+                try { if (elements.Length > 2) model.AFK = long.Parse(elements[2]); } catch { }
+                try { if (elements.Length > 3) model.CpuPer = int.Parse(elements[3]); } catch { }
+                try { if (elements.Length > 4) model.RamSize = long.Parse(elements[4]); } catch { }
+                try { if (elements.Length > 5) model.RamFree = long.Parse(elements[5]); } catch { }
+                try { if (elements.Length > 6) model.SysDriveSize = long.Parse(elements[6]); } catch { }
+                try { if (elements.Length > 7) model.SysDriveFree = long.Parse(elements[7]); } catch { }
+                try { if (elements.Length > 8) model.AppCpuPer = int.Parse(elements[8]); } catch { }
+                try { if (elements.Length > 9) model.AppRamUsed = double.Parse(elements[9]); } catch { }
+            }
+            return model;
+        }
+    }
+}

+ 3 - 6
Fork.Net/Azylee.Utils/Azylee.Core/WindowsUtils/InfoUtils/ComputerStatusTool.cs

@@ -9,21 +9,18 @@ namespace Azylee.Core.WindowsUtils.InfoUtils
     public class ComputerStatusTool
     {
         /// <summary>
-        /// CPU占用率
+        /// 获取 Processor(可获取CPU使用率)
         /// </summary>
         /// <returns></returns>
-        public static double CpuUtilization()
+        public static PerformanceCounter Processor()
         {
-            double value = 0;
             PerformanceCounter processor = null;
             try
             {
                 processor = new PerformanceCounter("Processor", "% Processor Time", "_Total");
-                value = processor.NextValue();
             }
             catch { }
-            finally { processor?.Dispose(); }
-            return value;
+            return processor;
         }
     }
 }

+ 0 - 11
Fork.Net/Fork.Net.sln

@@ -81,8 +81,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.BaiDuWebAPI", "Test\Te
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.RaiseOtherApp", "Test\Test.RaiseOtherApp\Test.RaiseOtherApp.csproj", "{79FCD171-33AC-47BF-B8A7-C19D365D2983}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azylee.BlackBox", "Azylee.Utils\Azylee.BlackBox\Azylee.BlackBox.csproj", "{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.BlackBox", "Test\Test.BlackBox\Test.BlackBox.csproj", "{6C69CF15-398D-4030-AA90-C617E52DC016}"
 EndProject
 Global
@@ -365,14 +363,6 @@ Global
 		{79FCD171-33AC-47BF-B8A7-C19D365D2983}.Release|Any CPU.Build.0 = Release|Any CPU
 		{79FCD171-33AC-47BF-B8A7-C19D365D2983}.Release|x86.ActiveCfg = Release|Any CPU
 		{79FCD171-33AC-47BF-B8A7-C19D365D2983}.Release|x86.Build.0 = Release|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Debug|x86.Build.0 = Debug|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Release|Any CPU.Build.0 = Release|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Release|x86.ActiveCfg = Release|Any CPU
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0}.Release|x86.Build.0 = Release|Any CPU
 		{6C69CF15-398D-4030-AA90-C617E52DC016}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{6C69CF15-398D-4030-AA90-C617E52DC016}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{6C69CF15-398D-4030-AA90-C617E52DC016}.Debug|x86.ActiveCfg = Debug|Any CPU
@@ -419,7 +409,6 @@ Global
 		{3936BB3B-244D-4448-9B77-3A7CC0CFBE28} = {A89FC45A-A907-4487-8719-114530A62684}
 		{3AB4B914-CCD0-48C9-8106-75A8FE542ACC} = {A89FC45A-A907-4487-8719-114530A62684}
 		{79FCD171-33AC-47BF-B8A7-C19D365D2983} = {A89FC45A-A907-4487-8719-114530A62684}
-		{6B84AB76-94B9-4DAE-B168-A6E216DD54F0} = {C34A95B5-5F83-46E3-868A-56BDDA2D0B87}
 		{6C69CF15-398D-4030-AA90-C617E52DC016} = {A89FC45A-A907-4487-8719-114530A62684}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution

+ 1 - 0
Fork.Net/Test/Test.BlackBox/Form1.Designer.cs

@@ -71,6 +71,7 @@
             this.Controls.Add(this.BTStartBB);
             this.Name = "Form1";
             this.Text = "Form1";
+            this.Load += new System.EventHandler(this.Form1_Load);
             this.ResumeLayout(false);
             this.PerformLayout();
 

+ 6 - 10
Fork.Net/Test/Test.BlackBox/Form1.cs

@@ -1,11 +1,5 @@
-using Azylee.BlackBox.Utils;
+using Azylee.Core.LogUtils.StatusLogUtils;
 using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-using System.Drawing;
-using System.Linq;
-using System.Text;
 using System.Windows.Forms;
 
 namespace Test.BlackBox
@@ -16,16 +10,18 @@ namespace Test.BlackBox
         {
             InitializeComponent();
         }
-
+        private void Form1_Load(object sender, EventArgs e)
+        {
+        }
         private void BTStartBB_Click(object sender, EventArgs e)
         {
-            bool flag = BlackBoxTool.Instance.Start();
+            bool flag = StatusLog.Instance.Start();
             textBox1.AppendText(Environment.NewLine + (flag ? "启动成功" : "启动失败"));
         }
 
         private void BTStopBB_Click(object sender, EventArgs e)
         {
-            bool flag = BlackBoxTool.Instance.Stop();
+            bool flag = StatusLog.Instance.Stop();
             textBox1.AppendText(Environment.NewLine + (flag ? "停止成功" : "停止失败"));
         }
     }

+ 0 - 4
Fork.Net/Test/Test.BlackBox/Test.BlackBox.csproj

@@ -74,10 +74,6 @@
     </Compile>
   </ItemGroup>
   <ItemGroup>
-    <ProjectReference Include="..\..\Azylee.Utils\Azylee.BlackBox\Azylee.BlackBox.csproj">
-      <Project>{6b84ab76-94b9-4dae-b168-a6e216dd54f0}</Project>
-      <Name>Azylee.BlackBox</Name>
-    </ProjectReference>
     <ProjectReference Include="..\..\Azylee.Utils\Azylee.Core\Azylee.Core.csproj">
       <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
       <Name>Azylee.Core</Name>