123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.IO;
- using System.Text;
- namespace HNWD.Pregrant.Common
- {
- public class NetLog
- {
- private static readonly object lockobj = new object();
- /// <summary>
- /// 写入日志到文本文件
- /// </summary>
- /// <param name="strMessage">日志内容</param>
- public static void WriteTextLog(string strMessage, string ok = "ok\\")
- {
- lock (lockobj)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\" + ok;
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- DateTime time = DateTime.Now;
- string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
- StringBuilder str = new StringBuilder();
- str.Append("Time:" + time + ";Message: " + strMessage + "\r\n");
- StreamWriter sw;
- if (!File.Exists(fileFullPath))
- {
- sw = File.CreateText(fileFullPath);
- }
- else
- {
- sw = File.AppendText(fileFullPath);
- }
- sw.WriteLine(str.ToString());
- sw.Close();
- }
- }
- /// <summary>
- /// 写入日志到文本文件
- /// </summary>
- /// <param name="strMessage">日志内容</param>
- public static void WriteTextOkLog(string strMessage)
- {
- lock (lockobj)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\Ok\\";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- DateTime time = DateTime.Now;
- string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
- StringBuilder str = new StringBuilder();
- str.Append("Time:" + time + ";Message: " + strMessage + "\r\n");
- StreamWriter sw;
- if (!File.Exists(fileFullPath))
- {
- sw = File.CreateText(fileFullPath);
- }
- else
- {
- sw = File.AppendText(fileFullPath);
- }
- sw.WriteLine(str.ToString());
- sw.Close();
- }
- }
- /// <summary>
- /// 写入日志到文本文件
- /// </summary>
- /// <param name="strMessage">日志内容</param>
- public static void WriteTextErrorLog(string strMessage)
- {
- lock (lockobj)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\Error\";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- DateTime time = DateTime.Now;
- string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
- StringBuilder str = new StringBuilder();
- str.Append("Time:" + time + ";Message: " + strMessage + "\r\n");
- StreamWriter sw;
- if (!File.Exists(fileFullPath))
- {
- sw = File.CreateText(fileFullPath);
- }
- else
- {
- sw = File.AppendText(fileFullPath);
- }
- sw.WriteLine(str.ToString());
- sw.Close();
- }
- }
- }
- }
|