欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

日志的类

程序员文章站 2022-06-21 09:49:09
public static class LogHelper { private static string strLogFolder; private static string strLogFile; private static object _obj = new object(); publi ......

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;
}
}
}
}