日志的类

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 + " ");
}
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}

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