創建Log日誌文本輸出範本

public static void Log(string strLogMsg, ApiLogType strLogType)
{
    //設定日誌路徑
    string strPath=HttpContext.Current.Server.MapPath("~/Accessary/Log/" + DateTime.Today.Year + "/" + strLogType.ToString());
    //判斷路徑是否存在,若不存在,則需要創建路徑
    if(!Directory.Exists(strPath))
    {
        Directory.CreateDirectory(strPath);
    }
    StreamWriter sw=null;
    try
        {
              //創建文本
            sw=File.AppendText(strPath+"\\API_"+ DateTime.Now.ToString("yyyyMMdd") +".txt");
            sw.WriteLine("消息內容:"+strLogMsg);
            sw.WriteLine("時間:"+DateTime.Now.Tostring("yyyy-MM-dd HH:mm:ss"));
            sw.WriteLine();
        }
    catch{}
    finally
            {
                if(sw!=null)
                {
                    sw.Flush();
                    sw.Close();
                }
            }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章