日期:2014-05-17  浏览次数:20845 次

记录日志
现在需要对webservice的每个接口都要记录日志,下面的代码合理吗?
请大家给我一点建议,谢谢。
C# code
private WebLog log=null;
[WebMethod]
Public DataSet ds()
{
log=new WebLog();
log.ServiceName="WebService";
log.StartTime=DateTime.Parse(System.DateTime.Now.ToString());
//连接数据库,最后返回ds
log.EndTime=DateTime.Parse(System.DateTime.Now.ToString());
WebWriteLog.Log(log);
return ds;
}
[WebMethod]
Public String str()
{
log=new WebLog();
log.ServiceName="WebService";
log.StartTime=DateTime.Parse(System.DateTime.Now.ToString());
//连接数据库,最后返回String
log.EndTime=DateTime.Parse(System.DateTime.Now.ToString());
LogWrite.LogWriteMethod(log);
return str;
}
//实体类:

//BLL写日志

public class LogWrite
{

    public static  int LogWriteMethod(Log log)
    {

        string sel="Insert into tb values (@StartTime,@ServiceName,@EndTime);";
        try
        {
            SqlParameter[] paras = new SqlParameter[] 
                                                {  
                                                    new SqlParameter("@StartTime", log.StartTime),
                                                    new SqlParameter("@ServiceName", log.ServiceName), 
                                                    new SqlParameter("@EndTime", log.EndTime)               
                                                };
            int rows = DbHelper.ExecuteNonQuery(sel, paras);
            return rows;
        }
        catch
        {
            return 0;
        }

        }
       
    }
DAL:
        public static int ExecuteNonQuery(string sql, SqlParameter[] paras)
        {
            try
            {
                using (SqlConnection conn= new SqlConnection())
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, con)
                    cmd.Parameters.AddRange(paras);
                    return cmd.ExecuteNonQuery();

                }
            }
            catch (Exception E)
            {

                throw E;
            }
        }





------解决方案--------------------
将日志提取一个方法出来,或者用委托将业务方法传进去