log4net I need to catch an error.


        public int ExecuteNonQuery()
        {
            int i = -1;
            try
            {
                Open();
                i = cmd.ExecuteNonQuery();
                Close();
            }
            catch (Exception ex)
            {
                if (handleErrors)
                    strLastError = ex.Message;
            }


            return i;
        }

I would like to put log4net if there is an error.
omegaloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RameshSCommented:
Try the following code.

Refer the following links.

Log error or exception using Log4Net

Log4Net Tutorial in C# .net (How can I show log in a file?)

how to log exception using log4net in asp.net application using c#

  public int ExecuteNonQuery()
        {
            int i = -1;
            try
            {
                Open();
                i = cmd.ExecuteNonQuery();
                Close();
            }
            catch (Exception ex)
            {
                if (handleErrors)
                    strLastError = ex.Message;

		ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

log.Error(ex.Message);
logger.Debug(ex.Message);
            }
            return i;
        }

Open in new window

0
omegaloveAuthor Commented:
Ramesh What I did was the following.
 catch (Exception ex)
        {
            if (log.IsErrorEnabled) { log.ErrorFormat("Exception occured executing a task: {0}", ex); }
            throw;
        }
Same thing?
0
RameshSCommented:
if you want to log error meesage to  log file, then   the statement if (log.IsErrorEnabled) { log.ErrorFormat("Exception occured executing a task: {0}", ex); } is sufficient.

If you want to throw the error to calling program in scenario like throwing execption from one layer to another layer, then put throw statement next to it.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

omegaloveAuthor Commented:
The thing I would like is that it will not display the error on the screen. If something fails.
0
RameshSCommented:
You don't want to display any error message if an exception occurred. the following is enough.

 catch (Exception ex)
        {
            if (log.IsErrorEnabled)
                {
                      log.ErrorFormat("Exception occured executing a task: {0}", ex);
                 }
        }

You need to watch the error log periodically to check if any error occurred.
0
omegaloveAuthor Commented:
so just remove the trow?
0
RameshSCommented:
Yes, you need to remove 'throw' statement. Becuase you have captured the exception using try and catch block and loged the error using log4net.  If you don't want to display anything such as waring/error messages to the user, just remove the  throw statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
omegaloveAuthor Commented:
awesome
0
omegaloveAuthor Commented:
k
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.