C#中简单的写日志的方法

 

public static void WriteLog(string msg)
        {
            FileStream fs = null;
            StreamWriter sw = null;

            try
            {
                var basePath = AppDomain.CurrentDomain.BaseDirectory;
                if (!basePath.EndsWith("/"))
                    basePath += "/";

                var logFile = basePath + DateTime.Now.ToString("yyyyMMdd") + "_debug.log";
                if (!File.Exists(logFile))
                    File.CreateText(logFile).Dispose();

                fs = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                sw = new StreamWriter(fs, Encoding.UTF8);

                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff"));
                sw.WriteLine(msg);
                sw.WriteLine("-------------------------------------");
                sw.Flush();
                fs.Flush();
            }
            catch { }
            finally
            {
                if (sw != null)
                {
                    try { sw.Close(); sw.Dispose(); } catch { }
                }
                if (fs != null)
                {
                    try { fs.Close(); fs.Dispose(); } catch { }
                }
            }
        }

 

public static void WriteLog(string msg)
        {
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string ServerPath = appPath + @"\Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

            StreamWriter srWriter = new StreamWriter(ServerPath, true);
            string WriteStr = "************************************************************************************************\r\n";
            WriteStr += "Date:" + DateTime.Now.ToString() + "  \r\n" + msg;
            srWriter.WriteLine(WriteStr);
            srWriter.Close();
        }

 

 

 

另附上js的提示的换行写法:

alert("您确定提交整个问卷吗?\n\n提交后将不能再填写各个工种的用工需求,整个问卷将就此结束!")

confirm("您确定提交整个问卷吗?\n\n提交后将不能再填写各个工种的用工需求,整个问卷将就此结束!")

 

 

 

 

//事物处理,catch部分是写日志的方法
        public static bool TransactionAction(string SQL, string TransactionName)
        {
            SqlConnection myConnection = new SqlConnection(SQLServer.SQLConnectionString);
            myConnection.Open();
            SqlCommand myCommand = myConnection.CreateCommand();
            SqlTransaction myTrans;
            myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted, TransactionName);
            myCommand.Connection = myConnection;
            myCommand.Transaction = myTrans;
            try
            {
                myCommand.CommandText = SQL;
                myCommand.ExecuteNonQuery();

                myTrans.Commit();
                return true;
            }
            catch (Exception ee)
            {
                string path = @"D:\webroot\ErrorLog";
                DirectoryInfo di = Directory.CreateDirectory(path); //在path下创建文件夹
                string strPath = path + "file://vipspace_error/_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
                StreamWriter srWriter = new StreamWriter(new FileStream(strPath, FileMode.OpenOrCreate), Encoding.GetEncoding("Unicode"));
                string WriteStr = "Date:" + DateTime.Now.ToString() + "  Error:" + ee.Message.ToString() + " SQL:" + SQL;
                srWriter.WriteLine(WriteStr);
                srWriter.Flush();
                srWriter.Close();

                myTrans.Rollback(TransactionName);
                return false;
            }
            finally
            {
                myConnection.Close();
            }
        }

 

 

 

判断文件是否存在,如存在并将其删除

public static bool IsExistsFile(string FilePath)
{
if (File.Exists(FilePath) == true)
{ 
//****删除文件
File.Delete(FilePath);
return true;
}
return false;

 

 

 

 

 

 

 

 

 

 

posted on 2014-06-05 16:34  itjeff  阅读(2085)  评论(0编辑  收藏  举报

导航