2

How to catch an error form log4net?

I have a program with log4net. The log4net is set a MySql server as repository. If it fail to connect to the MySql server, it will print many technical statements to console but I don't want to show them. Therefore, I want to catch the error and show "Fail to connect to the MySql" only.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
user554712
  • 211
  • 1
  • 3
  • 8

2 Answers2

2

log4net does not throw exceptions. Rather it writes all exceptions to error log internally with

EventLog.WriteEntry("ErrorHandler","Error: " + ex.Message, EventLogEntryType.Error);

So all you need is to read eventlog entries fit for your purposes.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • I do not need to write it to a log but I want to show a short message to user (show in console or a window). – user554712 May 15 '11 at 02:41
  • See the answer here: http://stackoverflow.com/questions/3542581/log4net-works-on-dev-machine-fails-when-deployed-to-shared-host-using-same-db-c/3543157#3543157 You'll need to enable debug mode and scan the Diagnostics.Trace entries for related error messages. – Teoman Soygul May 15 '11 at 02:47
2

Well one short simple workaround is that you can try connecting to mysql using the same connection string before calling the log4net.Configure() function and check if the connection is possible.

OR

simply follow what this email says if you want to ensure connectivity at all times i.e. create your own exceptionHandler Email

Email is very very old but i dont think such implementation is changed even in the latest version.

just my 2cents :)

Basit Anwer
  • 6,742
  • 7
  • 45
  • 88