I have a custom error logger that I want to:
1. Write a text file to a folder.
2. Using a web api, it sends an alert email to a staff members to notify that an error was written to a folder.
3. At the, end show the appropiate error view.
#1 and #3 worked fine before I introduced into the 'OnException' method code to process the #2. To do so, I had to add 'async' to the method as I am calling a web api that has that feature.
Now when MVC detects a code bug, it goes to the custom error logger but fails as it does not like the 'async' on the method.
I get:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
How do I solve this as I want to call the web api to send that alert email?
I have in the FilterConfig.cs - the ErrorLoggerAttribute points to a custom error logger.
- filters.Add(new ErrorLoggerAttribute());
Here is the custom ErrorLoggerAttribute.cs:
-
-
-
-
- public async override void OnException(ExceptionContext filterContext)
- {
-
-
-
-
-
- string strLogText = "";
- Exception ex = filterContext.Exception;
- filterContext.ExceptionHandled = true;
- var objClass = filterContext;
-
-
- strLogText += "Message ---\n{0}" + ex.Message;
-
-
- if (ex.Source == ".Net SqlClient Data Provider")
- {
- strLogText += Environment.NewLine + "SqlClient Error ---\n{0}" + "Check Sql Error";
- }
- else if (ex.Source == "System.Web.Mvc")
- {
- strLogText += Environment.NewLine + ".Net Error ---\n{0}" + "Check MVC Code For Error";
- }
- else if (filterContext.HttpContext.Request.IsAjaxRequest() == true)
- {
- strLogText += Environment.NewLine + ".Net Error ---\n{0}" + "Check MVC Ajax Code For Error";
- }
-
- strLogText += Environment.NewLine + "Source ---\n{0}" + ex.Source;
- strLogText += Environment.NewLine + "StackTrace ---\n{0}" + ex.StackTrace;
- strLogText += Environment.NewLine + "TargetSite ---\n{0}" + ex.TargetSite;
-
- if (ex.InnerException != null)
- {
-
- strLogText += Environment.NewLine + "Inner Exception is {0}" + ex.InnerException;
- }
- if (ex.HelpLink != null)
- {
-
- strLogText += Environment.NewLine + "HelpLink ---\n{0}" + ex.HelpLink;
- }
-
-
- StreamWriter log;
-
-
-
- string timestamp = DateTime.Now.ToString("d-MMMM-yyyy", new CultureInfo("en-GB"));
-
-
- string error_folder = ConfigurationManager.AppSettings["ClientErrorLogPath"].ToString();
-
-
- if (!System.IO.Directory.Exists(error_folder))
- {
- System.IO.Directory.CreateDirectory(error_folder);
- }
-
-
-
- if (!File.Exists(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp)))
- {
-
- log = new StreamWriter(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp));
- }
- else
- {
-
- log = File.AppendText(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp));
- }
-
- var controllerName = (string)filterContext.RouteData.Values["controller"];
- var actionName = (string)filterContext.RouteData.Values["action"];
-
-
-
- log.WriteLine(Environment.NewLine + DateTime.Now);
-
- log.WriteLine("------------------------------------------------------------------------------------------------");
- log.WriteLine("Controller Name :- " + controllerName);
- log.WriteLine("Action Method Name :- " + actionName);
- log.WriteLine("------------------------------------------------------------------------------------------------");
-
-
- log.WriteLine(objClass);
-
-
- log.WriteLine(strLogText);
-
-
- log.WriteLine();
-
-
-
-
-
-
- string errorMessage = "";
- string exceptionMessage = "";
-
- try
- {
-
- ControlResult controlResult = new ControlResult();
-
-
-
-
-
-
-
-
- controlResult = await GetControl();
-
-
- if (controlResult.ApiErrorMessage == null)
- {
-
-
- try
- {
-
-
-
-
-
-
- ClientErrorResult clientErrorResult = new ClientErrorResult();
-
-
- BLL_ClientError bll_ClientError = new BLL_ClientError();
-
-
-
- clientErrorResult = await bll_ClientError.ProcessClientErrorNoLog(controlResult.UserName);
-
-
- if (clientErrorResult.ApiErrorMessage != null)
- {
-
- errorMessage = controlResult.ApiErrorMessage;
- }
- }
- catch (Exception ex3)
- {
-
- errorMessage = "Server error on sending a client error alert. Exception error: " + ex3.Message;
- }
- }
- else
- {
-
- errorMessage = controlResult.ApiErrorMessage;
- }
- }
- catch (Exception ex1)
- {
-
- exceptionMessage = "Server error on getting. Exception error: " + ex1.Message;
-
- try
- {
-
- ClientErrorResult clientErrorResult = new ClientErrorResult();
-
-
-
-
- clientErrorResult = await ProcessClientError("Control", ex1.Message, "Server error on getting the control field. Method: OnException");
-
-
- if (clientErrorResult.ApiErrorMessage == null)
- {
-
-
- errorMessage = exceptionMessage;
- }
- else
- {
-
- errorMessage = clientErrorResult.ApiErrorMessage;
- }
- }
- catch (Exception ex2)
- {
-
- errorMessage = "Failure in ProcessClientError. Exception error: " + ex2.Message + ". Original error: " + exceptionMessage;
- }
- }
-
-
- if (errorMessage == "")
- {
-
- log.Close();
-
-
- filterContext.HttpContext.Session.Abandon();
-
-
- filterContext.Result = new ViewResult()
- {
-
- ViewName = "Error"
- };
- }
- else
- {
-
-
-
-
-
- log.WriteLine(Environment.NewLine + DateTime.Now);
-
-
- strLogText = "Attmpted to send a client error alert email to a staff member (an Admin) - but got an error. See it below.";
-
-
- log.WriteLine(strLogText);
-
-
- log.WriteLine();
-
-
- strLogText = "";
-
-
- strLogText += "Message ---\n{0}" + errorMessage;
-
-
- log.WriteLine(strLogText);
-
-
- log.WriteLine();
-
-
- log.Close();
-
-
- filterContext.HttpContext.Session.Abandon();
-
-
- filterContext.Result = new ViewResult()
- {
-
- ViewName = "ErrorSendingError"
- };
- }
- }
-
-
-
-
-
- public async Task<ControlResult> GetControl()
- {
- BLL_Control bll_Control = new BLL_Control();
-
- ControlResult controlResult = new ControlResult();
-
- try
- {
-
- controlResult = await bll_Control.GetControl();
- }
- catch (Exception)
- {
- throw;
- }
-
- return controlResult;
- }
-
-
-
-
-
- public async Task<ClientErrorResult> ProcessClientError(string userName, string errorMessage, string additionalInfo)
- {
- BLL_ClientError bll_ClientError = new BLL_ClientError();
-
- ClientErrorResult clientErrorResult = new ClientErrorResult();
-
- try
- {
-
- clientErrorResult = await bll_ClientError.ProcessClientError(userName, errorMessage, additionalInfo);
- }
- catch (Exception)
- {
- throw;
- }
-
- return clientErrorResult;
- }