ELMAH - Exception Logging without having HttpContext

前端 未结 4 1472
野的像风
野的像风 2020-12-13 03:40

I tried this solution with Elmah.XmlFileErrorLog but I\'m getting following exception

System.ArgumentNullException was unhandled by user code
  Message=\"Val         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 04:08

    Thanks everyone, here is my ErrorHelper class I came up with to manually log errors on websites and in WCF services, windows services, etc:

        public static class ErrorHelper
    {
        /// 
        /// Manually log an exception to Elmah.  This is very useful for the agents that try/catch all the errors.
        /// 
        /// In order for this to work elmah must be setup in the web.config/app.config file
        /// 
        /// 
        public static void LogErrorManually(Exception ex)
        {
            if (HttpContext.Current != null)//website is logging the error
            {                
                var elmahCon = Elmah.ErrorSignal.FromCurrentContext();
                elmahCon.Raise(ex);
            }
            else//non website, probably an agent
            {                
                var elmahCon = Elmah.ErrorLog.GetDefault(null);
                elmahCon.Log(new Elmah.Error(ex));
            }
        }
    }
    

提交回复
热议问题