{"id":881,"date":"2021-09-25T08:48:33","date_gmt":"2021-09-25T08:48:33","guid":{"rendered":"http:\/\/gongyikun.cn\/?p=881"},"modified":"2021-09-25T08:48:43","modified_gmt":"2021-09-25T08:48:43","slug":"c%e5%86%99%e6%97%a5%e5%bf%97%e6%96%87%e4%bb%b6%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.gongyikun.cn\/?p=881","title":{"rendered":"C#\u5199\u65e5\u5fd7\u6587\u4ef6\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\u5728\u5f00\u53d1\uff0c\u6709\u65f6\u4e3a\u4e86\u9a8c\u8bc1\u7a0b\u5e8f\u8fd0\u884c\u662f\u5426\u6b63\u786e\uff0c\u901a\u5e38\u8981\u5199\u65e5\u5fd7\u6765\u8bb0\u5f55\u64cd\u4f5c\uff0c\u5728\u4e00\u4e2a\u65e5\u5fd7\u7c7b\u91cc\uff0c\u901a\u5e38\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<h3>1\u3001\u4ee5\u65e5\u671f\u4f5c\u4e3a\u65e5\u5fd7\u6587\u4ef6\u7684\u540d\u79f0<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public void WriteLog(string msg)  \n{  \n    string filePath = AppDomain.CurrentDomain.BaseDirectory + \"Log\";  \n    if (!Directory.Exists(filePath))  \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(\"\u6d88\u606f\uff1a\" + msg);  \n            sw.WriteLine(\"\u65f6\u95f4\uff1a\" + DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\"));  \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(\"**************************************************\");  \n            sw.WriteLine();  \n            sw.Flush();  \n            sw.Close();  \n            sw.Dispose();  \n        }  \n    }  \n}  \n<\/code><\/pre>\n<h3>2\u3001\u4ee5\u6587\u6863\u540d\u79f0\u548c\u65e5\u671f\u7ed3\u5408\u4f5c\u4e3a\u6587\u4ef6\u540d<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public void WriteLog(string documentName, string msg)  \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.Close();  \n    swLogFile.Dispose();  \n}\n<\/code><\/pre>\n<div class=\"clearfix\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1\uff0c\u6709\u65f6\u4e3a\u4e86\u9a8c\u8bc1\u7a0b\u5e8f\u8fd0\u884c\u662f\u5426\u6b63\u786e\uff0c\u901a\u5e38\u8981\u5199\u65e5\u5fd7\u6765\u8bb0\u5f55\u64cd\u4f5c\uff0c\u5728\u4e00\u4e2a\u65e5\u5fd7\u7c7b\u91cc\uff0c\u901a\u5e38\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a 1\u3001\u4ee5\u65e5\u671f\u4f5c\u4e3a [&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\/881"}],"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=881"}],"version-history":[{"count":2,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions\/883"}],"wp:attachment":[{"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gongyikun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}