自定义Log 写到文件中
Posted 君凌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Log 写到文件中相关的知识,希望对你有一定的参考价值。
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.IO;
- using System.Text;
- /// <summary>
- /// Summary description for NetLog
- /// </summary>
- public class NetLog
- {
- /// <summary>
- /// 写入日志到文本文件
- /// </summary>
- /// <param name="action">动作</param>
- /// <param name="strMessage">日志内容</param>
- /// <param name="time">时间</param>
- public static void WriteTextLog(string action, string strMessage, DateTime time)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + @"System\Log\";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
- StringBuilder str = new StringBuilder();
- str.Append("Time: " + time.ToString() + "\r\n");
- str.Append("Action: " + action + "\r\n");
- str.Append("Message: " + strMessage + "\r\n");
- str.Append("-----------------------------------------------------------\r\n\r\n");
- StreamWriter sw;
- if (!File.Exists(fileFullPath))
- {
- sw = File.CreateText(fileFullPath);
- }
- else
- {
- sw = File.AppendText(fileFullPath);
- }
- sw.WriteLine(str.ToString());
- sw.Close();
- }
- }
以上是关于自定义Log 写到文件中的主要内容,如果未能解决你的问题,请参考以下文章