首页 > 其他 > 详细

日志的类

时间:2019-07-17 16:36:59      阅读:76      评论:0      收藏:0      [点我收藏+]

public static class LogHelper
{
private static string strLogFolder;
private static string strLogFile;
private static object _obj = new object();
public static void CreatFile()
{
lock (_obj)
{
try
{
strLogFolder = Directory.GetCurrentDirectory() + "\\Log";
if (!Directory.Exists(strLogFolder))
{
Directory.CreateDirectory(strLogFolder);
}
strLogFile = strLogFolder + "\\" + DateTime.Now.ToString("yy-MM-dd")+".Log";
if (!File.Exists(strLogFile))
{
File.Create(strLogFile);
}
}
catch (Exception ex)
{
throw ex;
}

}
}

public static void WriteMessage(string msg)
{
lock(_obj)
{
try
{
strLogFile = strLogFolder + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".Log";
if (!File.Exists(strLogFile))
{
File.Create(strLogFile);
}
using (StreamWriter sw = new StreamWriter(strLogFile, true, Encoding.UTF8))
{
sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg + "\r\n");
}
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}

日志的类

原文:https://www.cnblogs.com/lao-K/p/11201236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!