Click here to Skip to main content
13,052,696 members (64,737 online)
Rate this:
Please Sign up or sign in to vote.
My application has following structure,

MyApp - C# Console Application
ClsMessage - C# Class Library
ClsLog - C# Class Library

I am calling ClsMessage class from MyApp to display message. ClsMessage checks
If application type is Windows then it display message box.
If application type is console then ClsMessage class calls ClsLog class to write message into log file. I am using log4Net for generating log. I want to trap error if log4Net is not configured properly.
When ClsMessage class calls ClsLog class, in ClsLog constructor I am using the following command to confiture the log4Net.


If I did't properly confitured log4Net in App.config file, above line generate an error and display that error in console. But I want to trap that error and log that error in event log.
I am using the following code in ClsLog class constructor.
static Log()
    catch (Exception ex)
       throw ex;

The above constructor can't catch the exception.
Posted 20-Jan-11 11:18am
Updated 20-Jan-11 13:32pm
Manfred R. Bihy 20-Jan-11 19:43pm
I modified my answer please see there!
Sandeep Mewara 21-Jan-11 0:42am
No idea why downvoted. Countered!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your "constructor" has a try catch block and re-throws the exception if one was caught. So where do you think the mistake is? What would you have expected instead?

Please explain!

If the configuration of log4net fails, it will write output about that failure to the console. So if you still want us to help us it would be really great if you modified (use "Improve Question" link) your question to include the full error message that is dumped to the console. This is most likely an issue with your configuration file. You still have to explain though why you are re-throwing the exception you caught.
End of modification

Marcus Kramer 20-Jan-11 19:33pm
OP comment moved from answer:
Above line is giving me error which is good but it display the error in console instead of catch the exception.
Manfred R. Bihy 20-Jan-11 19:36pm
Thanks Marcus!
SAKryukov 20-Jan-11 21:19pm
Manfred, I voted "5" just for your ability to handle such brain damage.
In fact I don't know how to help really. It should be something like "Unlearn what you have learned, Luke".
Zia Mehdi 21-Jan-11 11:34am
Thanks Manfred,
Yes you are right. Error is in my configuration file. But thats the thing which I want to catch. There is no log4Net tag in my configuration file. If user doesn't configured log4Net properly then I want to catch the exception in ClsLog and throw that exception to ClsMessage. ClsMessage then write an event log for this error. But ClsLog can't catch the exception, I mean it is not going into catch block but shows the error in console and executes normally.

Error is below:

log4Net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the applicatio's .config file. Check your .config file for the <log4net> and <configsections> elements. The configuration section should look like:
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

[Adding to Manfred's response.]

If your problem is that log4net logs errors to the console before you get a chance to handle any exceptions, then check to see if it has any means to disable console error logging. It would be quite surprising if it won't let you do that. And isn't it open source? If so you also have the final option of modifying the source to prevent this error logging from happening, although I'd think it would be very unlikely that you'd need to do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Jan 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100