110,534
社区成员
发帖
与我相关
我的任务
分享
class Log
{
//方法1
//public static string path= System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//方法2
public static string path = @"E:\Project\XX酒店\TSTcpServer\TcpServer\obj\Debug";
public static void WriteLog(string type, string className, string content)
{
try
{
if (!Directory.Exists(path))//如果日志目录不存在就创建
{
Directory.CreateDirectory(path);
}
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//获取当前系统时间
string filename = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
//创建或打开日志文件,向日志文件末尾追加记录
StreamWriter mySw = File.AppendText(filename);
//向日志文件写入内容
string write_content = time + " " + type + " " + className + ": " + content;
mySw.WriteLine(write_content);
//关闭日志文件
mySw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
}
System.AppDomain.CurrentDomain.BaseDirectory