How to determine the exception is a SQLException in Application_Error in global

Priya
Priya
1194 Points
33 Posts

In asp.net application, I'm trying to determine the exception of type SQLException so that if any db exception occurs we log error or so. I have following section in Global.asax.cs

void Application_Error(object sender, EventArgs e)
        {
            var _logger = LogManager.GetCurrentClassLogger();
            var ex = HttpContext.Current.Server.GetLastError();

            if (ex is HttpException)
            {
                if ((ex as HttpException).GetHttpCode() != 404)
                {
                    _logger.Fatal(ex, ex == null ? "" : ex.Message);
                }
            }
            else if (ex is type of SqlException )
            {
                _logger.Fatal(ex, ex == null ? "" : ex.Message);
            }
            else
            {
                _logger.Fatal(ex, ex == null ? "" : ex.Message);
            }

 

Views: 1145
Total Answered: 1
Total Marked As Answer: 1
Posted On: 08-Jan-2022 05:01

Share:   fb twitter linkedin
Answers
Brian
Brian
2376 Points
13 Posts
         

Try something:

 else if (ex.GetType() == typeof(System.Data.SqlClient.SqlException))
            {
                _logger.Fatal(ex, ex == null ? "" : ex.Message);
            }
Posted On: 12-Jan-2022 23:18
Thanks..
 - Priya  01-Feb-2022 23:08
 Log In to Chat