Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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.

log4net.Config.XmlConfigurator.Configure();

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.
C#
static Log()
{
    try
    {
       log4net.Config.XmlConfigurator.Configure();
    }
    catch (Exception ex)
    {
       throw ex;
    }
}

The above constructor can't catch the exception.
Posted
Updated 20-Jan-11 13:32pm
v3
Comments
Manfred Rudolf 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!

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!

Modification:
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

Regards,
Manfred
 
Share this answer
 
v2
Comments
fjdiewornncalwe 20-Jan-11 19:33pm    
OP comment moved from answer:
log4net.Config.XmlConfigurator.Configure();
Above line is giving me error which is good but it display the error in console instead of catch the exception.
Manfred Rudolf Bihy 20-Jan-11 19:36pm    
Thanks Marcus!
Sergey Alexandrovich Kryukov 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:
[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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900