{"id":887,"date":"2021-09-27T03:41:20","date_gmt":"2021-09-27T03:41:20","guid":{"rendered":"http:\/\/gongyikun.cn\/?p=887"},"modified":"2021-10-07T01:41:38","modified_gmt":"2021-10-07T01:41:38","slug":"c%e6%b7%bb%e5%8a%a0%e4%bf%a1%e6%81%af%e5%88%b0listview%e6%9c%aa%e5%ae%8c","status":"publish","type":"post","link":"https:\/\/blog.gongyikun.cn\/?p=887","title":{"rendered":"C#\u6dfb\u52a0\u4fe1\u606f\u5230ListView\uff0c\u5e76\u8f93\u51faLog\u65e5\u5fd7"},"content":{"rendered":"<h3>\u6dfb\u52a0\u5230listView\uff0c\u5199\u5165\u5230log\u65e5\u5fd7\u6587\u4ef6<\/h3>\n<pre><code class=\"language-csharp line-numbers\"> public void AddInfo(string log,InfoSeverity s,bool outFile = true)\n        {\n            string[] str = new string[3];\n            str[0] = listView1.Items.Count.ToString();\n            str[1] = DateTime.Now.ToString();\n            str[2] = log;\n            ListViewItem lst = new ListViewItem(str);\n            switch (s)\n            {\n                case InfoSeverity.Error:\n                    lst.BackColor = Color.White;\/\/\u80cc\u666f\u8272\n                    lst.ForeColor = Color.Red;\/\/\u524d\u666f\u8272\n                    break;\n                case InfoSeverity.Info:\n                    lst.BackColor = Color.White;\/\/\u80cc\u666f\u8272\n                    lst.ForeColor = Color.Blue;\/\/\u524d\u666f\u8272\n                    break;\n                case InfoSeverity.Warning:\n                    lst.BackColor = Color.Yellow;\n                    lst.ForeColor = Color.Black;\/\/\u524d\u666f\u8272\n                    break;\n                default:\n                    break;\n            }\n            \/\/\u8ba9\u6700\u65b0\u4ea7\u751f\u7684\u4fe1\u606f\u663e\u793a\u5728\u7b2c\u4e00\u884c\n            this.listView1.Items.Insert(0, lst);\n            listView1.Items[0].EnsureVisible();\n\n            \/\/\u662f\u5426\u9700\u8981\u8f93\u51fa\u5230\u6587\u4ef6\n            if (outFile)\n            {\n                WriteLog.WriteLogToFile(\"Log\" ,log, (WriteLog.InfoSeverity)(Convert.ToInt32(s)));\n            }\n        }\n<\/code><\/pre>\n<h3>\u5199\u6587\u4ef6\u7c7b\u7684\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-csharp line-numbers\"> public static class WriteLog\n    {\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u6d88\u606f\u4e25\u91cd\u6027\uff0c\u679a\u4e3e\n        \/\/\/ &lt;\/summary&gt;\n        public enum InfoSeverity\n        {\n            Error,\n            Info,\n            Warning\n        }\n\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u5199\u65e5\u5fd7\u5230\u6587\u4ef6\uff0c\u6587\u4ef6\u4f4d\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u540c\u7ea7\u76ee\u5f55\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"msg\"&gt;\u9700\u8981\u5199\u5165\u7684\u65e5\u5fd7\u5185\u5bb9&lt;\/param&gt;\n        public static void WriteLogToFile(string msg, InfoSeverity severity)\n        {\n            string filePath = AppDomain.CurrentDomain.BaseDirectory + \"Log\";\n            if (!Directory.Exists(filePath))\/\/\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\n            {\n                Directory.CreateDirectory(filePath);\n            }\n            string logPath = AppDomain.CurrentDomain.BaseDirectory + \"Log\\\\\" + DateTime.Now.ToString(\"yyyy-MM-dd\") + \".txt\";\n            try\n            {\n                using (StreamWriter sw = File.AppendText(logPath))\n                {\n                    \/\/sw.WriteLine(DateTime.Now.ToString(\"HH:mm:ss\") + \"\\t\" + msg);\n                    sw.WriteLine(\"\u6d88\u606f     \uff1a\" + msg);\n                    sw.WriteLine(\"\u65f6\u95f4     \uff1a\" + DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\"));\n                    sw.WriteLine(\"\u4e25\u91cd\u6027   \uff1a\" + severity.ToString());\n                    sw.WriteLine(\"**************************************************\");\n                    sw.WriteLine();\n                    sw.Flush();\n                    sw.Close();\n                    sw.Dispose();\n                }\n            }\n            catch (IOException e)\n            {\n                using (StreamWriter sw = File.AppendText(logPath))\n                {\n                    sw.WriteLine(\"\u5f02\u5e38     \uff1a\" + e.Message);\n                    sw.WriteLine(\"\u65f6\u95f4     \uff1a\" + DateTime.Now.ToString(\"yyy-MM-dd HH:mm:ss\"));\n                    sw.WriteLine(\"\u4e25\u91cd\u6027   \uff1aError\" );\n                    sw.WriteLine(\"**************************************************\");\n                    sw.WriteLine();\n                    sw.Flush();\n                    sw.Close();\n                    sw.Dispose();\n                }\n            }\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u91cd\u8f7d\u51fd\u6570\uff1a\n        \/\/\/ \u5199\u65e5\u5fd7\u5230\u6587\u4ef6\uff0c\u6587\u4ef6\u4f4d\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u540c\u7ea7\u76ee\u5f55\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"documentName\"&gt;\u65e5\u5fd7\u6587\u4ef6\u7684\u540d\u79f0&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"msg\"&gt;\u9700\u8981\u5199\u5165\u7684\u65e5\u5fd7\u5185\u5bb9&lt;\/param&gt;\n        public static void WriteLogToFile(string documentName, string msg, InfoSeverity severity)\n        {\n            string errorLogFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, \"Log\");\n            if (!System.IO.Directory.Exists(errorLogFilePath))\n            {\n                System.IO.Directory.CreateDirectory(errorLogFilePath);\n            }\n            string logFile = System.IO.Path.Combine(errorLogFilePath, documentName + \"@\" + DateTime.Today.ToString(\"yyyy-MM-dd\") + \".txt\");\n            bool writeBaseInfo = System.IO.File.Exists(logFile);\n            StreamWriter swLogFile = new StreamWriter(logFile, true, Encoding.Unicode);\n            \/\/swLogFile.WriteLine(DateTime.Now.ToString(\"HH:mm:ss\") + \"\\t\" + msg);\n            swLogFile.WriteLine(\"\u6d88\u606f     \uff1a\" + msg);\n            swLogFile.WriteLine(\"\u65f6\u95f4     \uff1a\" + DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\"));\n            swLogFile.WriteLine(\"\u4e25\u91cd\u6027   \uff1a\" + severity.ToString());\n            swLogFile.WriteLine(\"**************************************************\");\n            swLogFile.Close();\n            swLogFile.Dispose();\n        }\n    }\n<\/code><\/pre>\n<div class=\"clearfix\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u6dfb\u52a0\u5230listView\uff0c\u5199\u5165\u5230log\u65e5\u5fd7\u6587\u4ef6 public void AddInfo(string log,I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts\/887"}],"collection":[{"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=887"}],"version-history":[{"count":3,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}