Monday

c# custom webservice logging and error handling

//step1.declare delagate
public delegate T UnSafeProcedure<T>();

/*step2. add performance and error handling methods */
        public string Concat(params object[] arguments)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in arguments)
            {
                sb.AppendFormat("\"{0}\",", item);
            }
            return sb.ToString();
        }

        public T RunSafe<T>(UnSafeProcedure<T> s, params object[] arguments)
        {
            DateTime st = DateTime.Now;
            try
            {
               return  s();
            }
            catch (Exception e1)
            {
                {
                    Debug.Write("Error," + Concat(arguments) + "\r\n" + e1.ToString());
                }
                return default(T);
            }
            finally
            {
                var sec = DateTime.Now.Subtract(st).TotalSeconds;
                if (sec > 20)
                {
                    string tm = ">20sec,start," + st.ToLongTimeString() + ",End," + DateTime.Now.ToLongTimeString();
                    Debug.Write(tm+ Concat(arguments));
                }
            }
        }

/*3.performance counting procedures will looks like:*/

public Receipt VerifyUser(int Id, string username, string password)
{
           return  RunSafe<Receipt>(delegate()
            {
                //internal procedure that needs to be chcked troubleshooted
                using (var service = Instance())
                {
                    return service.VerifyAccount(Id, username, password);
                }

            }, "VerifyUser,facilityId",Id,"username",username,"password",password);
}

1 comment:

admin said...

or parametreless function will be :
public void RunSafe(UnSafeVoid s, params object[] arguments)
{
DateTime st = DateTime.Now;
try
{
s();
}
catch (Exception e1)
{
{
log("Error," + Concat(arguments) + "\r\n" + e1.ToString());
}

}
finally
{


}
}
public delegate void UnSafeVoid();

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...