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();
}
}asp.net写入日志到文本文件
最新推荐文章于 2026-05-10 12:09:28 发布
本文介绍了一个使用C#编写的简单日志记录系统,该系统可以将包含时间戳、动作及消息的日志条目写入指定日期的文本文件中。文章提供了完整的源代码,并解释了如何创建和追加日志文件。

2261

被折叠的 条评论
为什么被折叠?



