46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using System.IO;
|
||
using System.Text;
|
||
|
||
namespace ZWL.Common
|
||
{
|
||
public static class SaveLog
|
||
{
|
||
public static void Logs(string 文件路径, string 文件名称, string 内容)
|
||
{
|
||
try
|
||
{
|
||
//文件夹存在验证
|
||
if (!Directory.Exists(文件路径))
|
||
{
|
||
Directory.CreateDirectory(文件路径);
|
||
}
|
||
//默认添加日志日期前缀
|
||
StringBuilder logName = new StringBuilder();
|
||
string date = System.DateTime.Today.ToString("yyyy-MM-dd" + "-");
|
||
logName.Append(date);
|
||
if (!string.IsNullOrEmpty(文件名称))
|
||
{
|
||
logName.Append(文件名称);
|
||
}
|
||
logName.Append(".log");
|
||
string filePath = 文件路径 + "\\" + logName.ToString();
|
||
FileStream fs;
|
||
if (!File.Exists(filePath))
|
||
{
|
||
fs = File.Create(filePath);
|
||
}
|
||
else
|
||
{
|
||
fs = File.Open(filePath, FileMode.Append);
|
||
}
|
||
//内容前默认添加时间
|
||
string strToWrite = System.DateTime.Now.ToString() + ":" + 内容 + "\r\n";
|
||
byte[] b = System.Text.Encoding.Default.GetBytes(strToWrite);
|
||
fs.Write(b, 0, b.Length);
|
||
fs.Close();
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
}
|