c#写日志常用函数

@c#
private static object lockLogTxt = new object();`

`public static void Logtest(string logstring)
{
try
{
lock (lockLogTxt)
{
string path = AppDomain.CurrentDomain.BaseDirectory + “WebApplog.txt”;
//写入日志
using (StreamWriter writer = new StreamWriter(path, true))
{
int i = logstring.IndexOf(“HeartConnect”);
if (-1 != i)
{
return;
}
writer.WriteLine(logstring);
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return;
}
}

public static void DeleteLogTxt() //templeBiaoji 暂时先按大小自动删除,后面要改成按日期删除
{
try
{
lock (lockLogTxt)
{
string path = AppDomain.CurrentDomain.BaseDirectory + “WebApplog.txt”;
long size = 0;
//获取文件大小
using (FileStream file = System.IO.File.OpenRead(path))
{
size = file.Length;//文件大小。byte
}

                //判断日志文件大于32M,自动删除。
                if (size > (32*1024 * 1024))
                {
                    System.IO.File.Delete(path);
                }
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43232671/article/details/88820504