日志方法

 

private static void WriteLog(string errMsg)
{
    //web程序下取得根目录
    //string sFilePath = HttpContext.Current.Request.PhysicalApplicationPath + "\\DebugLog";
    //非web程序取得根目录
    string sFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\DebugLog";
    string sFileName = "Look.log";
    sFileName = sFilePath + "\\" + sFileName;
    Directory.CreateDirectory(sFilePath);
    FileStream fs;
    StreamWriter sw;
    if (File.Exists(sFileName))
        fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
    else
        fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
    sw = new StreamWriter(fs);
    sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    sw.WriteLine("信息:" + errMsg);
    sw.WriteLine("");
    sw.Close();
    fs.Close();
}

获取根目录方式详情:https://www.cnblogs.com/leixiaoling/archive/2012/03/30/2425466.html

 

posted @ 2021-07-19 11:31  kueizheng  阅读(43)  评论(0编辑  收藏  举报