标签:string filePath logPath 记录 sw ToString WriteLine net 日志
.net中 最常用的日志记录一般都知道log4net,相对需要引用dll和一些配置,其实可以最简单的自己生成日志,为了方便以后自己的使用,在此记录下
public static class Logger
{
public static void WriteLog(string msg)
{
string filePath = "";
string logPath = "";
try
{
string LogDir = ConfigurationManager.AppSettings["LogDir"].ToString();//文件名
filePath = @"C:\Log\" + LogDir + @"\"; //路径
logPath = filePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine("消息:" + msg);
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
catch (IOException e)
{
using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine("异常:" + e.Message);
sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
}
在其他类中直接调用即可
Logger.WriteLog("这里是要记录的日志!");
标签:string,filePath,logPath,记录,sw,ToString,WriteLine,net,日志 来源: https://blog.csdn.net/ycstephenlmj/article/details/118853540
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。