Erik_Brooks | 1 Nov 21:47 2005

newbie - log4net deletes my config file!


I'm a .net and log4net newbie working thru the tutorial at http://www.codeproject.com/csharp/log4net_intro.asp

First example, logging to console with no config file, works fine. 2nd example, using a small config file, I see two problems.
The main problem is that my config file is deleted from the system when I run the project.  Here is the complete code of the project:
using System;
using log4net;

namespace log4netConsoleTest
{
        public class MainTestClass
        {
                private static  ILog logger = LogManager.GetLogger(typeof (MainTestClass));

        // constructor:
                static MainTestClass ()
                {
                        log4net.Config.XmlConfigurator.Configure();
                }
               
                static void Main(string[] args)
                {
                        logger.Debug("Here is a debug log.");
                        logger.Error("... and a non-fatal error.");
                        logger.Fatal("... and a fatal error.");
                }
        }
}
 
When I look into the file directory where the exe file lives, I see that file "log4netConsoleTest.exe.config' exists. When I run the project and stop at the 'LogManager.GetLogger' line, and then look again into the file directory, the config file is gone!   If I restore the file into the directory at that time, then the logging works as expected.

if I don't restore the file, log4net write this to the console:
"log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />"
I think that is also a problem. It apparently can't find the file (cause it doesn't exist), but instead of saying 'file not found', it says it can't find certain chars inside the file.  I'd like the error message to include the fully qualified name of where it looked for the file.

All help appreciated,
Erik Brooks


-----------------------------------------------
IMPORTANT NOTIFICATION
-----------------------------------------------
The contents of this e-mail and its attachments are confidential and may be privileged. If you are not the intended recipient of this e-mail, please notify IDX immediately (by return e-mail to either the sender or security <at> idx.com), destroy all copies of this message along with any attachments and do not disclose, copy and/or distribute the contents. The views expressed in this message are those of the author and not necessarily those of IDX. In the absence of a prior written agreement with you authorizing commitments of IDX via e-mail, the above message shall not bind IDX, unless from a duly authorized officer of the company in a context indicating an intention to bind the company.

This e-mail and its attachments are protected by copyright and other laws. (c) IDX Systems Corporation 2005. All rights reserved. IDX is a registered trademark of IDX Investment Corporation.
Nicko Cadell | 1 Nov 23:15 2005

RE: newbie - log4net deletes my config file!

Actually Visual Studio is deleting your config file.

Create a file called App.config in the root of your VisualStudio
project.
Visual Studio will automagically copy this file into the build output
directory and rename it to log4netConsoleTest.exe.config.
Put your log4net configuration in the App.config file.

As for not finding the file; When you call XmlConfigurator.Configure()
then log4net calls
System.Configuration.ConfigurationSettings.GetConfig("log4net"). If this
returns null then it prints out the message you are seeing. It does not
know where the file is or try to guess, it just asks the .NET
configuration API for its configuration. There are other ways of calling
XmlConfigurator.Configure(), for example passing a FileInfo param, which
will cause log4net to look for a specific file, then it will print out a
file not found message.

Cheers,
Nicko

> -----Original Message-----
> From: Erik_Brooks <at> idx.com [mailto:Erik_Brooks <at> idx.com] 
> Sent: 01 November 2005 20:48
> To: log4net-user <at> logging.apache.org
> Subject: newbie - log4net deletes my config file!
> 
> 
> I'm a .net and log4net newbie working thru the tutorial at 
> http://www.codeproject.com/csharp/log4net_intro.asp 
> 
> First example, logging to console with no config file, works 
> fine. 2nd example, using a small config file, I see two problems. 
> The main problem is that my config file is deleted from the 
> system when I run the project.  Here is the complete code of 
> the project: 
> using System;
> using log4net; 
> 
> namespace log4netConsoleTest
> { 
>         public class MainTestClass 
>         { 
>                 private static  ILog logger = 
> LogManager.GetLogger(typeof (MainTestClass)); 
> 
>         // constructor: 
>                 static MainTestClass () 
>                 { 
>                         log4net.Config.XmlConfigurator.Configure(); 
>                 } 
>                 
>                 static void Main(string[] args) 
>                 { 
>                         logger.Debug("Here is a debug log."); 
>                         logger.Error("... and a non-fatal error."); 
>                         logger.Fatal("... and a fatal error."); 
>                 } 
>         }
> } 
>   
> When I look into the file directory where the exe file lives, 
> I see that file "log4netConsoleTest.exe.config' exists. When 
> I run the project and stop at the 'LogManager.GetLogger' 
> line, and then look again into the file directory, the config 
> file is gone!   If I restore the file into the directory at 
> that time, then the logging works as expected. 
> 
> if I don't restore the file, log4net write this to the console: 
> "log4net:ERROR XmlConfigurator: Failed to find configuration 
> section 'log4net' in the application's .config file. Check 
> your .config file for the <log4net> and <configSections> 
> elements. The configuration section should look like: 
> <section name="log4net" 
> type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />" 
> I think that is also a problem. It apparently can't find the 
> file (cause it doesn't exist), but instead of saying 'file 
> not found', it says it can't find certain chars inside the 
> file.  I'd like the error message to include the fully 
> qualified name of where it looked for the file. 
> 
> All help appreciated,
> Erik Brooks
> 
> 
> -----------------------------------------------
> IMPORTANT NOTIFICATION
> -----------------------------------------------
> The contents of this e-mail and its attachments are 
> confidential and may be privileged. If you are not the 
> intended recipient of this e-mail, please notify IDX 
> immediately (by return e-mail to either the sender or 
> security <at> idx.com), destroy all copies of this message along 
> with any attachments and do not disclose, copy and/or 
> distribute the contents. The views expressed in this message 
> are those of the author and not necessarily those of IDX. In 
> the absence of a prior written agreement with you authorizing 
> commitments of IDX via e-mail, the above message shall not 
> bind IDX, unless from a duly authorized officer of the 
> company in a context indicating an intention to bind the company. 
> 
> This e-mail and its attachments are protected by copyright 
> and other laws. (c) IDX Systems Corporation 2005. All rights 
> reserved. IDX is a registered trademark of IDX Investment Corporation.
> 
> 

Erik_Brooks | 2 Nov 01:24 2005

RE: newbie - log4net deletes my config file!


Thanks for the clue, Nicko,

I found that in addition to placing the app.config file within the file system, I also had to tell Visual Studio to add a file to the project. Only then did it do the automagic that Nicko mentioned.
 
Erik

-----------------------------------------------
IMPORTANT NOTIFICATION
-----------------------------------------------
The contents of this e-mail and its attachments are confidential and may be privileged. If you are not the intended recipient of this e-mail, please notify IDX immediately (by return e-mail to either the sender or security <at> idx.com), destroy all copies of this message along with any attachments and do not disclose, copy and/or distribute the contents. The views expressed in this message are those of the author and not necessarily those of IDX. In the absence of a prior written agreement with you authorizing commitments of IDX via e-mail, the above message shall not bind IDX, unless from a duly authorized officer of the company in a context indicating an intention to bind the company.

This e-mail and its attachments are protected by copyright and other laws. (c) IDX Systems Corporation 2005. All rights reserved. IDX is a registered trademark of IDX Investment Corporation.
Russell Haley | 2 Nov 19:41 2005

High occurrence errors

Hello again to the log4net group!

I am currently using L4N to log all the errors in a real time data
acquisition application and have come across a bit of a problem. There are
certain places in the application where exceptions occur at a very high rate
when a problem does arise and the log files generated become huge in a
matter of minutes (or in one case, seconds). The specific places that I have
found this to be problematic is in a serial driver and in the class that
stores the data to a database. 

Although L4N can limit the size of the files and the number of "roll overs",
I (read "My Boss") would also like to limit the rate at which these
exceptions are actually logged. Is there any built in mechanism that I can
use to achieve this? I would still like to know that the errors are
occurring, but in the cases that I am thinking of, I don't need to know that
one or two exceptions occurred 35 times in one second! I am looking at
implementing some sort of "buffer" that holds the past x errors and has a
threshold that defines the time period between exceptions logged, but again,
I was wondering if there was anything internal to L4N to achieve this?

I'm well aware that I need to eliminate the problematic exceptions, but I
still need to have some mechanism to find out when they occur. In the case
of the serial driver, it has been running very well but the other day I
tried using a Serial to USB dongle (converter) that caused problems. I have
since eliminated that specific problem, but as I'm sure everyone is aware,
these things are not always foreseeable.

Any suggestions would be greatly appreciated. Thank you once again for your
help.

Sincerely,

Russell Haley

Attachment (winmail.dat): application/ms-tnef, 3121 bytes
Ron Grabowski | 2 Nov 21:06 2005
Picon

Re: High occurrence errors

This will generate a lot of messages with exceptions then generate a
few exceptions more slowly:

 for (int i=0; i < 10000; i++)
 {
  log.Debug(
   "ApplicationException " + i, 
   new ApplicationException("ApplicationException " + i));
 }
 System.Threading.Thread.Sleep(5000); // 5 seconds
 log.Debug(
  "ApplicationException " + DateTime.Now, 
  new ApplicationException("ApplicationException " + DateTime.Now));
 System.Threading.Thread.Sleep(5000); // 5 seconds
 log.Debug(
  "ApplicationException " + DateTime.Now, 
  new ApplicationException("ApplicationException " + DateTime.Now));
 System.Threading.Thread.Sleep(5000); // 5 seconds
 log.Debug(
  "ApplicationException " + DateTime.Now, 
  new ApplicationException("ApplicationException " + DateTime.Now));

It sounds like you only want to process the logging event if the
exception occured more than a specified interval from the last
exception of the same type? This output is from a filter that accepts a
logging event only if the logging event was at least 2 seconds from the
previous logging event with the same exception:

DEBUG 2005-11-02 03:07:26 PM - ApplicationException 0
System.ApplicationException: ApplicationException 0
DEBUG 2005-11-02 03:07:28 PM - ApplicationException 2555
System.ApplicationException: ApplicationException 2555
DEBUG 2005-11-02 03:07:30 PM - ApplicationException 5440
System.ApplicationException: ApplicationException 5440
DEBUG 2005-11-02 03:07:32 PM - ApplicationException 8333
System.ApplicationException: ApplicationException 8333
DEBUG 2005-11-02 03:07:38 PM - ApplicationException 11/2/2005 3:07:38
PM
System.ApplicationException: ApplicationException 11/2/2005 3:07:38 PM
DEBUG 2005-11-02 03:07:43 PM - ApplicationException 11/2/2005 3:07:43
PM
System.ApplicationException: ApplicationException 11/2/2005 3:07:43 PM
DEBUG 2005-11-02 03:07:48 PM - ApplicationException 11/2/2005 3:07:48
PM
System.ApplicationException: ApplicationException 11/2/2005 3:07:48 PM

Threshold probably isn't the best name for the internval property:

 <filter type="Company.Project.Logging.ExceptionThrottleFilter">
  <threshold value="2" />
  <exceptionType value="System.ApplicationException" />
 </filter>

See the attached class for how I implemented it. You should be able to
chain the filters too. A faster implementation may be to subtract Ticks
instead of using DateTime's Subtract method.

Another approach would be to change the ExceptionObject to just the
string representation of its type:

 loggingEvent.ExceptionObject = loggingEventObject.GetType().ToString()

if some criteria is met. That would allow you to keep the exceptions
while reducing the size of the log files. This could be done in as a
Layout or a Converter.

- Ron

--- Russell Haley <rhaley <at> axys.com> wrote:

> Hello again to the log4net group!
> 
> I am currently using L4N to log all the errors in a real time data
> acquisition application and have come across a bit of a problem.
> There are
> certain places in the application where exceptions occur at a very
> high rate
> when a problem does arise and the log files generated become huge in
> a
> matter of minutes (or in one case, seconds). The specific places that
> I have
> found this to be problematic is in a serial driver and in the class
> that
> stores the data to a database. 
> 
> Although L4N can limit the size of the files and the number of "roll
> overs",
> I (read "My Boss") would also like to limit the rate at which these
> exceptions are actually logged. Is there any built in mechanism that
> I can
> use to achieve this? I would still like to know that the errors are
> occurring, but in the cases that I am thinking of, I don't need to
> know that
> one or two exceptions occurred 35 times in one second! I am looking
> at
> implementing some sort of "buffer" that holds the past x errors and
> has a
> threshold that defines the time period between exceptions logged, but
> again,
> I was wondering if there was anything internal to L4N to achieve
> this?
> 
> I'm well aware that I need to eliminate the problematic exceptions,
> but I
> still need to have some mechanism to find out when they occur. In the
> case
> of the serial driver, it has been running very well but the other day
> I
> tried using a Serial to USB dongle (converter) that caused problems.
> I have
> since eliminated that specific problem, but as I'm sure everyone is
> aware,
> these things are not always foreseeable.
> 
> Any suggestions would be greatly appreciated. Thank you once again
> for your
> help.
> 
> Sincerely,
> 
> Russell Haley
> 
> 
public class ExceptionThrottleFilter : FilterSkeleton
{
	private DateTime lastException = DateTime.MinValue;
	private Type exceptionType = typeof(Exception);
	private int threshold = 5; // seconds

	public override void ActivateOptions()
	{
		base.ActivateOptions();
	}

	public override FilterDecision Decide(LoggingEvent loggingEvent)
	{
		if (loggingEvent.ExceptionObject != null && loggingEvent.ExceptionObject.GetType() == exceptionType)
		{
			if (loggingEvent.TimeStamp.Subtract(lastException).TotalSeconds > threshold)
			{
				lastException = loggingEvent.TimeStamp;
				return FilterDecision.Accept;
			}
			else
			{
				return FilterDecision.Deny;
			}
		}
		else
		{
			return FilterDecision.Neutral;
		}
	}

	public Type ExceptionType
	{
		get { return exceptionType; }
		set { exceptionType = value; }
	}

	public int Threshold
	{
		get { return threshold; }
		set { threshold = value; }
	}
}
Shireesh Thanneru | 3 Nov 01:37 2005
Picon

How to get the log level and other config info programmatically

How do I get the current log level and other config info programmatically? The ILogger interface does not define any methods or properties to do this.
 
More specifically, if I have a logger like this:
 

private

static readonly ILog logger = LogManager.getLogger();

 

How do I find out the log level of this logger (as set in the config file) and also the log level of the root logger? logger.IsInfoEnabled, etc. will suffice in most cases, but how do I find out what is the exact log level that is specified in the config file?

 

Thanks,

 

Shireesh Thanneru

Shireesh Thanneru | 3 Nov 04:14 2005
Picon

Re: How to get the log level and other config info programmatically

Sorry, I meant 
private static readonly ILog logger = LogManager.getLogger(typeof(MyClass));

Shireesh Thanneru <thanneru <at> yahoo.com> wrote:
How do I get the current log level and other config info programmatically? The ILogger interface does not define any methods or properties to do this.
 
More specifically, if I have a logger like this:
 

private

static readonly ILog logger = LogManager.getLogger();

 

How do I find out the log level of this logger (as set in the config file) and also the log level of the root logger? logger.IsInfoEnabled, etc. will suffice in most cases, but how do I find out what is the exact log level that is specified in the config file?

 

Thanks,

 

Shireesh Thanneru

Göran Roseen | 3 Nov 09:34 2005
Picon

RE: How to get the log level and other config info programmatically

 

Well, the ILog interface does have the IsXXXXEnabled methods, which will solve most situations where you need to address current log level.

 

i.e.

if (log.IsDebugEnabled)

{

     log.Debug("Entry number: " + i + " is " + entry[i]);

}

 

/Göran

 

From: Shireesh Thanneru [mailto:thanneru <at> yahoo.com]
Sent: den 3 november 2005 04:15
To: Log4NET User
Subject: Re: How to get the log level and other config info programmatically

 

Sorry, I meant 

private static readonly ILog logger = LogManager.getLogger(typeof(MyClass));

Shireesh Thanneru <thanneru <at> yahoo.com> wrote:

How do I get the current log level and other config info programmatically? The ILogger interface does not define any methods or properties to do this.

 

More specifically, if I have a logger like this:

 

private static readonly ILog logger = LogManager.getLogger();

 

How do I find out the log level of this logger (as set in the config file) and also the log level of the root logger? logger.IsInfoEnabled, etc. will suffice in most cases, but how do I find out what is the exact log level that is specified in the config file?

 

Thanks,

 

Shireesh Thanneru

Blake Bishop | 4 Nov 00:20 2005

Re: High occurrence errors

Ron,

The concepts behind your solution to the throttling problem would make a 
great addition to the log4net documentation IMHO, possibly under some new 
documentation topic/subtopic like "Filter Examples/Logging Every Nth 
Message" or something like that.

Cheers,
Blake

"Ron Grabowski" <rongrabowski <at> yahoo.com> wrote in message 
news:20051102200616.93984.qmail <at> web32209.mail.mud.yahoo.com...
> This will generate a lot of messages with exceptions then generate a
> few exceptions more slowly:
>
> for (int i=0; i < 10000; i++)
> {
>  log.Debug(
>   "ApplicationException " + i,
>   new ApplicationException("ApplicationException " + i));
> }
> System.Threading.Thread.Sleep(5000); // 5 seconds
> log.Debug(
>  "ApplicationException " + DateTime.Now,
>  new ApplicationException("ApplicationException " + DateTime.Now));
> System.Threading.Thread.Sleep(5000); // 5 seconds
> log.Debug(
>  "ApplicationException " + DateTime.Now,
>  new ApplicationException("ApplicationException " + DateTime.Now));
> System.Threading.Thread.Sleep(5000); // 5 seconds
> log.Debug(
>  "ApplicationException " + DateTime.Now,
>  new ApplicationException("ApplicationException " + DateTime.Now));
>
> It sounds like you only want to process the logging event if the
> exception occured more than a specified interval from the last
> exception of the same type? This output is from a filter that accepts a
> logging event only if the logging event was at least 2 seconds from the
> previous logging event with the same exception:
>
> DEBUG 2005-11-02 03:07:26 PM - ApplicationException 0
> System.ApplicationException: ApplicationException 0
> DEBUG 2005-11-02 03:07:28 PM - ApplicationException 2555
> System.ApplicationException: ApplicationException 2555
> DEBUG 2005-11-02 03:07:30 PM - ApplicationException 5440
> System.ApplicationException: ApplicationException 5440
> DEBUG 2005-11-02 03:07:32 PM - ApplicationException 8333
> System.ApplicationException: ApplicationException 8333
> DEBUG 2005-11-02 03:07:38 PM - ApplicationException 11/2/2005 3:07:38
> PM
> System.ApplicationException: ApplicationException 11/2/2005 3:07:38 PM
> DEBUG 2005-11-02 03:07:43 PM - ApplicationException 11/2/2005 3:07:43
> PM
> System.ApplicationException: ApplicationException 11/2/2005 3:07:43 PM
> DEBUG 2005-11-02 03:07:48 PM - ApplicationException 11/2/2005 3:07:48
> PM
> System.ApplicationException: ApplicationException 11/2/2005 3:07:48 PM
>
> Threshold probably isn't the best name for the internval property:
>
> <filter type="Company.Project.Logging.ExceptionThrottleFilter">
>  <threshold value="2" />
>  <exceptionType value="System.ApplicationException" />
> </filter>
>
> See the attached class for how I implemented it. You should be able to
> chain the filters too. A faster implementation may be to subtract Ticks
> instead of using DateTime's Subtract method.
>
> Another approach would be to change the ExceptionObject to just the
> string representation of its type:
>
> loggingEvent.ExceptionObject = loggingEventObject.GetType().ToString()
>
> if some criteria is met. That would allow you to keep the exceptions
> while reducing the size of the log files. This could be done in as a
> Layout or a Converter.
>
> - Ron
>
> --- Russell Haley <rhaley <at> axys.com> wrote:
>
>> Hello again to the log4net group!
>>
>> I am currently using L4N to log all the errors in a real time data
>> acquisition application and have come across a bit of a problem.
>> There are
>> certain places in the application where exceptions occur at a very
>> high rate
>> when a problem does arise and the log files generated become huge in
>> a
>> matter of minutes (or in one case, seconds). The specific places that
>> I have
>> found this to be problematic is in a serial driver and in the class
>> that
>> stores the data to a database.
>>
>> Although L4N can limit the size of the files and the number of "roll
>> overs",
>> I (read "My Boss") would also like to limit the rate at which these
>> exceptions are actually logged. Is there any built in mechanism that
>> I can
>> use to achieve this? I would still like to know that the errors are
>> occurring, but in the cases that I am thinking of, I don't need to
>> know that
>> one or two exceptions occurred 35 times in one second! I am looking
>> at
>> implementing some sort of "buffer" that holds the past x errors and
>> has a
>> threshold that defines the time period between exceptions logged, but
>> again,
>> I was wondering if there was anything internal to L4N to achieve
>> this?
>>
>> I'm well aware that I need to eliminate the problematic exceptions,
>> but I
>> still need to have some mechanism to find out when they occur. In the
>> case
>> of the serial driver, it has been running very well but the other day
>> I
>> tried using a Serial to USB dongle (converter) that caused problems.
>> I have
>> since eliminated that specific problem, but as I'm sure everyone is
>> aware,
>> these things are not always foreseeable.
>>
>> Any suggestions would be greatly appreciated. Thank you once again
>> for your
>> help.
>>
>> Sincerely,
>>
>> Russell Haley
>>
>>

--------------------------------------------------------------------------------

> public class ExceptionThrottleFilter : FilterSkeleton
> {
> private DateTime lastException = DateTime.MinValue;
> private Type exceptionType = typeof(Exception);
> private int threshold = 5; // seconds
>
> public override void ActivateOptions()
> {
> base.ActivateOptions();
> }
>
> public override FilterDecision Decide(LoggingEvent loggingEvent)
> {
> if (loggingEvent.ExceptionObject != null && 
> loggingEvent.ExceptionObject.GetType() == exceptionType)
> {
> if (loggingEvent.TimeStamp.Subtract(lastException).TotalSeconds > 
> threshold)
> {
> lastException = loggingEvent.TimeStamp;
> return FilterDecision.Accept;
> }
> else
> {
> return FilterDecision.Deny;
> }
> }
> else
> {
> return FilterDecision.Neutral;
> }
> }
>
> public Type ExceptionType
> {
> get { return exceptionType; }
> set { exceptionType = value; }
> }
>
> public int Threshold
> {
> get { return threshold; }
> set { threshold = value; }
> }
> } 

Shireesh Thanneru | 4 Nov 04:35 2005
Picon

Re: How to get the log level and other config info programmatically

Can some one answer this question? Does Log4Net expose config information through APIs?

Shireesh Thanneru <thanneru <at> yahoo.com> wrote:
How do I get the current log level and other config info programmatically? The ILogger interface does not define any methods or properties to do this.
 
More specifically, if I have a logger like this:
 

private

static readonly ILog logger = LogManager.getLogger(typeof(MyClass));

 

How do I find out the log level of this logger (as set in the config file) and also the log level of the root logger? logger.IsInfoEnabled, etc. will suffice in most cases, but how do I find out what is the exact log level that is specified in the config file?

 

Thanks,

 

Shireesh Thanneru


Gmane