ソースを参照

测试Log组件的日志等级策略

yuzhengyang 8 年 前
コミット
e09d0bfced

+ 4 - 2
Fork.Net/Waka.Lego/Commons/R.cs

@@ -1,6 +1,7 @@
 using System;
 using System;
 using System.Windows.Forms;
 using System.Windows.Forms;
-using Y.Utils.ComputerUtils;
+using Y.Utils.AppUtils;
+using Y.Utils.IOUtils.LogUtils;
 
 
 namespace Waka.Lego.Commons
 namespace Waka.Lego.Commons
 {
 {
@@ -15,6 +16,7 @@ namespace Waka.Lego.Commons
         public static string MarksFile;//应用记录文件文件名
         public static string MarksFile;//应用记录文件文件名
         public static bool Release = false;
         public static bool Release = false;
         public static string Name = Environment.MachineName;
         public static string Name = Environment.MachineName;
-        public static bool IsAdministrator = ComputerPermissionTool.IsAdministrator();
+        public static bool IsAdministrator = PermissionTool.IsAdministrator();
+        public static Log Log = new Log();
     }
     }
 }
 }

+ 5 - 3
Fork.Net/Waka.Lego/Program.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Windows.Forms;
+using Waka.Lego.Views;
 using Y.Utils.ReflectionUtils.AttributeUtils;
 using Y.Utils.ReflectionUtils.AttributeUtils;
 
 
 namespace Waka.Lego
 namespace Waka.Lego
@@ -17,9 +18,10 @@ namespace Waka.Lego
         {
         {
             Application.EnableVisualStyles();
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.SetCompatibleTextRenderingDefault(false);
-            Form1 form = new Form1();
-            ControlAttribute.Band(form);
-            Application.Run(form);
+            //Form1 form = new Form1();
+            //ControlAttribute.Band(form);
+            //Application.Run(form);
+            Application.Run(new MainForm());
         }
         }
     }
     }
 }
 }

+ 92 - 0
Fork.Net/Waka.Lego/Views/MainForm.Designer.cs

@@ -0,0 +1,92 @@
+namespace Waka.Lego.Views
+{
+    partial class MainForm
+    {
+        /// <summary>
+        /// Required designer variable.
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        /// Required method for Designer support - do not modify
+        /// the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
+            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
+            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
+            this.dataGridView1 = new System.Windows.Forms.DataGridView();
+            this.PluginList = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
+            this.SuspendLayout();
+            // 
+            // dataGridView1
+            // 
+            this.dataGridView1.AllowUserToAddRows = false;
+            this.dataGridView1.AllowUserToDeleteRows = false;
+            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
+            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
+            this.PluginList});
+            this.dataGridView1.Location = new System.Drawing.Point(12, 12);
+            this.dataGridView1.Name = "dataGridView1";
+            this.dataGridView1.ReadOnly = true;
+            dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
+            dataGridViewCellStyle5.BackColor = System.Drawing.SystemColors.Control;
+            dataGridViewCellStyle5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            dataGridViewCellStyle5.ForeColor = System.Drawing.SystemColors.WindowText;
+            dataGridViewCellStyle5.SelectionBackColor = System.Drawing.SystemColors.Highlight;
+            dataGridViewCellStyle5.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
+            dataGridViewCellStyle5.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
+            this.dataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle5;
+            this.dataGridView1.RowHeadersVisible = false;
+            dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
+            this.dataGridView1.RowsDefaultCellStyle = dataGridViewCellStyle6;
+            this.dataGridView1.RowTemplate.Height = 23;
+            this.dataGridView1.Size = new System.Drawing.Size(415, 328);
+            this.dataGridView1.TabIndex = 0;
+            // 
+            // PluginList
+            // 
+            this.PluginList.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
+            dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
+            this.PluginList.DefaultCellStyle = dataGridViewCellStyle4;
+            this.PluginList.HeaderText = "插件列表";
+            this.PluginList.Name = "PluginList";
+            this.PluginList.ReadOnly = true;
+            // 
+            // MainForm
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(439, 352);
+            this.Controls.Add(this.dataGridView1);
+            this.Name = "MainForm";
+            this.Text = "MainForm";
+            this.Load += new System.EventHandler(this.MainForm_Load);
+            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
+            this.ResumeLayout(false);
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.DataGridView dataGridView1;
+        private System.Windows.Forms.DataGridViewTextBoxColumn PluginList;
+    }
+}

+ 35 - 0
Fork.Net/Waka.Lego/Views/MainForm.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+using Waka.Lego.Commons;
+using Y.Utils.IOUtils.LogUtils;
+
+namespace Waka.Lego.Views
+{
+    public partial class MainForm : Form
+    {
+        public MainForm()
+        {
+            InitializeComponent();
+        }
+
+        private void MainForm_Load(object sender, EventArgs e)
+        {
+            R.Log.IsWriteFile = true;
+            R.Log.LogLevel = LogLevel.Warning | LogLevel.Debug;
+            Log.AllocConsole();
+
+            R.Log.v("this is v 啰嗦");
+            R.Log.d("this is d 调试");
+            R.Log.i("this is i 重要");
+            R.Log.w("this is w 警告");
+            R.Log.e("this is e 错误");
+        }
+    }
+}

+ 123 - 0
Fork.Net/Waka.Lego/Views/MainForm.resx

@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <metadata name="PluginList.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+</root>

+ 9 - 0
Fork.Net/Waka.Lego/Waka.Lego.csproj

@@ -64,6 +64,12 @@
     </Compile>
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Views\MainForm.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="Views\MainForm.Designer.cs">
+      <DependentUpon>MainForm.cs</DependentUpon>
+    </Compile>
     <EmbeddedResource Include="Form1.resx">
     <EmbeddedResource Include="Form1.resx">
       <DependentUpon>Form1.cs</DependentUpon>
       <DependentUpon>Form1.cs</DependentUpon>
     </EmbeddedResource>
     </EmbeddedResource>
@@ -76,6 +82,9 @@
       <AutoGen>True</AutoGen>
       <AutoGen>True</AutoGen>
       <DependentUpon>Resources.resx</DependentUpon>
       <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
     </Compile>
+    <EmbeddedResource Include="Views\MainForm.resx">
+      <DependentUpon>MainForm.cs</DependentUpon>
+    </EmbeddedResource>
     <None Include="Properties\app.manifest" />
     <None Include="Properties\app.manifest" />
     <None Include="Properties\Settings.settings">
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <Generator>SettingsSingleFileGenerator</Generator>

+ 11 - 10
Fork.Net/Y.Utils/IOUtils/LogUtils/Log.cs

@@ -23,9 +23,10 @@ namespace Y.Utils.IOUtils.LogUtils
         const string LogFormat = "{0}  {1}  {2}";
         const string LogFormat = "{0}  {1}  {2}";
         const string TimeFormat = "HH:mm:ss.fff";
         const string TimeFormat = "HH:mm:ss.fff";
 
 
-        public static bool IsWriteFile = true;//是否写日志文件
-        private static object LogFileLock = new object();//写日志文件锁
-        private static LogLevel LogLevel = LogLevel.All;//日志输出等级
+        private object LogFileLock = new object();//写日志文件锁
+
+        public bool IsWriteFile = true;//是否写日志文件
+        public LogLevel LogLevel = LogLevel.All;//日志输出等级
 
 
         #region Console 开启/关闭 API
         #region Console 开启/关闭 API
         [DllImport("kernel32.dll")]
         [DllImport("kernel32.dll")]
@@ -58,14 +59,14 @@ namespace Y.Utils.IOUtils.LogUtils
         /// <param name="type">类型</param>
         /// <param name="type">类型</param>
         /// <param name="tag">标记</param>
         /// <param name="tag">标记</param>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
-        private static void Write(LogType type, string message)
+        private void Write(LogType type, string message)
         {
         {
             Console.ForegroundColor = GetColor(type);
             Console.ForegroundColor = GetColor(type);
             Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
             Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
             if (IsWriteFile) WriteFile(type, message);
             if (IsWriteFile) WriteFile(type, message);
         }
         }
 
 
-        private static void WriteFile(LogType type, string message)
+        private void WriteFile(LogType type, string message)
         {
         {
             if (IsWriteFile)
             if (IsWriteFile)
             {
             {
@@ -88,7 +89,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// </summary>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
         /// <param name="tag">可选:标记</param>
         /// <param name="tag">可选:标记</param>
-        public static void v<T>(T msg)
+        public void v<T>(T msg)
         {
         {
             if ((LogLevel & LogLevel.Verbose) == LogLevel.Verbose)
             if ((LogLevel & LogLevel.Verbose) == LogLevel.Verbose)
                 Write(LogType.v, msg.ToString());
                 Write(LogType.v, msg.ToString());
@@ -98,7 +99,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// </summary>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
         /// <param name="tag">可选:标记</param>
         /// <param name="tag">可选:标记</param>
-        public static void d<T>(T msg)
+        public void d<T>(T msg)
         {
         {
             if ((LogLevel & LogLevel.Debug) == LogLevel.Debug)
             if ((LogLevel & LogLevel.Debug) == LogLevel.Debug)
                 Write(LogType.d, msg.ToString());
                 Write(LogType.d, msg.ToString());
@@ -108,7 +109,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// </summary>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
         /// <param name="tag">可选:标记</param>
         /// <param name="tag">可选:标记</param>
-        public static void i<T>(T msg)
+        public void i<T>(T msg)
         {
         {
             if ((LogLevel & LogLevel.Information) == LogLevel.Information)
             if ((LogLevel & LogLevel.Information) == LogLevel.Information)
                 Write(LogType.i, msg.ToString());
                 Write(LogType.i, msg.ToString());
@@ -118,7 +119,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// </summary>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
         /// <param name="tag">可选:标记</param>
         /// <param name="tag">可选:标记</param>
-        public static void w<T>(T msg)
+        public void w<T>(T msg)
         {
         {
             if ((LogLevel & LogLevel.Warning) == LogLevel.Warning)
             if ((LogLevel & LogLevel.Warning) == LogLevel.Warning)
                 Write(LogType.w, msg.ToString());
                 Write(LogType.w, msg.ToString());
@@ -128,7 +129,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// </summary>
         /// <param name="message">消息</param>
         /// <param name="message">消息</param>
         /// <param name="tag">可选:标记</param>
         /// <param name="tag">可选:标记</param>
-        public static void e<T>(T msg)
+        public void e<T>(T msg)
         {
         {
             if ((LogLevel & LogLevel.Error) == LogLevel.Error)
             if ((LogLevel & LogLevel.Error) == LogLevel.Error)
                 Write(LogType.e, msg.ToString());
                 Write(LogType.e, msg.ToString());

+ 1 - 3
Fork.Net/Y.Utils/ReflectionUtils/ReflectionCoreUtils/DomainTool.cs

@@ -24,9 +24,7 @@ namespace Y.Utils.ReflectionUtils.ReflectionCoreUtils
                 return appDomain;
                 return appDomain;
             }
             }
             catch (Exception e)
             catch (Exception e)
-            {
-                Log.e(e.Message);
-            }
+            { }
             return null;
             return null;
         }
         }
         public static T CreateInstance<T>(AppDomain domain)
         public static T CreateInstance<T>(AppDomain domain)

+ 1 - 3
Fork.Net/Y.Utils/ReflectionUtils/ReflectionCoreUtils/SimpleReflection.cs

@@ -35,9 +35,7 @@ namespace Y.Utils.ReflectionUtils.ReflectionCoreUtils
                 }
                 }
             }
             }
             catch (Exception e)
             catch (Exception e)
-            {
-                Log.e(e.Message);
-            }
+            { }
             return null;
             return null;
         }
         }