Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Application log4net
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.
   static Log()
   {
       try
       {
          log4net.Config.XmlConfigurator.Configure();
       }
       catch (Exception ex)
       {
          throw ex;
       }
   }
The above constructor can't catch the exception.
Posted 20-Jan-11 12:18pm
Edited 20-Jan-11 14:32pm
v3
Comments
Manfred R. Bihy at 20-Jan-11 19:43pm
   
I modified my answer please see there!
Sandeep Mewara at 21-Jan-11 0:42am
   
No idea why downvoted. Countered!
Rate this: bad
good
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!
 
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
  Permalink  
v2
Comments
Marcus Kramer at 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 R. Bihy at 20-Jan-11 19:36pm
   
Thanks Marcus!
SAKryukov at 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 at 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 and elements. The configuration section should look like:
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 565
1 Maciej Los 319
2 Richard MacCutchan 220
3 BillWoodruff 164
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Jan 2011
Copyright © CodeProject, 1999-2014
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