You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Macarthur Work <ma...@gmail.com> on 2010/08/25 16:17:03 UTC

EventLogLogger doesn’t work

Hello,

I have library without any xml file:

public class Log4NetEventLogger
    {
        #region Private Instance Fields
        private string _loggerName;

        private log4net.ILog _logger;


        #endregion Private Instance Fields

        #region constructor
        public Log4NetEventLogger(string logerName)
        {

            _loggerName = logerName;

            _logger = log4net.LogManager.GetLogger(_loggerName);


                if (_logger.Logger.Repository.GetAppenders().Where(a =>
a.Name == "EventLogAppender").Count() == 0)
                {
                    AddAppender(logerName,
CreateEventLogAppender("EventLogAppender"));
                }
                 }
        #endregion constructor

        public void AddEntry(string contextInfo, string message,
EventLogEntryType eventType)
        {

            _logger = log4net.LogManager.GetLogger(_loggerName);

            switch (eventType)
            {
                case EventLogEntryType.Error:
                    _logger.Error(message);
                    break;
                case EventLogEntryType.Warning:
                    _logger.Warn(message);
                    break;
                case EventLogEntryType.Information:
                    _logger.Info(message);
                    break;
                case EventLogEntryType.SuccessAudit:
                    throw new NotImplementedException();
                    break;
                case EventLogEntryType.FailureAudit:
                    throw new NotImplementedException();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("eventType");
            }

        public static void AddAppender(string loggerName,
log4net.Appender.IAppender appender)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l =
          (log4net.Repository.Hierarchy.Logger)log.Logger;

            l.AddAppender(appender);
        }

        private IAppender CreateEventLogAppender(string loggerName)
        {
            log4net.Appender.EventLogAppender appender = new
           log4net.Appender.EventLogAppender();
            appender.Name = "EventLogAppender";
            appender.ApplicationName = loggerName;
            appender.LogName = "MyDiary";

            log4net.Layout.PatternLayout layout = new
          log4net.Layout.PatternLayout();
            layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
            layout.ActivateOptions();

            appender.Layout = layout;
            appender.ActivateOptions();

                 var filter = new log4net.Filter.LoggerMatchFilter();



            filter.AcceptOnMatch = true;



            var filterDeny = new log4net.Filter.DenyAllFilter();



            var filterErrorType = new log4net.Filter.LevelRangeFilter();



            filterErrorType.AcceptOnMatch = true;



            //filterErrorType.LevelMin = Level.Error;



            appender.AddFilter(filter);

            appender.AddFilter(filterDeny);

            appender.AddFilter(filterErrorType);



            appender.ActivateOptions();



            log4net.Config.BasicConfigurator.Configure(appender);


            return appender;
        }
    }
}

I use it like this:

  var myLogger = new Log4NetEventLogger("calculator");
myLogger.AddEntry("exception", "additionalInformation",
EventLogEntryType.Error);

 I use windows xp as Administrator

It creates me new eventlog, but it doesn't add any new item to it. Why is
that ?

Thanks & Regards,

Re: Too many log files

Posted by Michael Schall <mi...@gmail.com>.
I just created my own appender in a new assembly. That way I could roll back to the stock implementation if needed. 

Mike

On May 26, 2011, at 11:09 AM, "Lansdaal, Michael T" <mi...@boeing.com> wrote:

> Thanks for posting that (I don’t follow the mailing list too closely and missed it).  I like the current implementation if your patch that has MaxDateRollBackups of 4 keeping
> 
>  
> 
> foo.2010-06-17.log
> 
> foo.2010-06-16.log
> 
> foo.2010-06-15.log
> 
> foo.2010-06-15.1.log
> 
> foo.2010-06-15.2.log
> 
> foo.2010-06-14.log
> 
>  
> 
> I guess the question would be – are the developers interested in folding this into the main branch or are we stuck with making our own patched version?
> 
>  
> 
> Thanks, Mike
> 
>  
> 
>  
> 
> From: Michael Schall [mailto:mike.schall@gmail.com] 
> Sent: Tuesday, May 24, 2011 9:57 AM
> To: Log4NET User
> Subject: Re: Too many log files
> 
>  
> 
> I worked on a solution to this that we are using in production...  I posted it on the list without much response...  You are welcome to try this...
> 
>  
> 
> http://article.gmane.org/gmane.comp.apache.logging.log4net.devel/885/match=rolling+files+date+time+boundaries+doesn't+support+maximum+number+backup
> 
> On Tue, May 24, 2011 at 11:38 AM, Rob Richardson <Ro...@rad-con.com> wrote:
> 
> Thanks for your reply!  I quote a reply to that thread below:
> 
>  
> 
> I suspect the difficulties deleting files rolled only by date is that
> the appender would need to take into account the DatePattern used when
> locating files to be considered for deleting. Counted backups
> (log.txt.1, log.txt.2, log.txt.3, etc.) are easier to deal with because
> integer sequences are more well-known.
> 
> I'm not aware of any efforts to support rolling of files based on
> RollingMode.Date.
> 
>  
> 
> I would be happy with counted backups, as mentioned in that reply.  How do I set my rolling file appender to do that?
> 
>  
> 
> Thanks again!
> 
>  
> 
> RobR
> 
>  
> 
> From: Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com] 
> Sent: Tuesday, May 24, 2011 12:33 PM
> To: Log4NET User
> Subject: RE: Too many log files
> 
>  
> 
> This post (http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F17575%2Fview%2Ftopic%2FDefault.aspx) says that maxSizeRollBackups does not work if you set RollingStyle to RollingMode.Date (and another post I saw says it as “rollingStyle of Date does not support maxSizeRollBackups).
> 
>  
> 
> I have implemented my own directory cleaning code in application to clean out log files older than a certain date.
> 
>  
> 
> Thanks, Mike
> 
>  
> 
> From: Rob Richardson [mailto:Rob.Richardson@rad-con.com] 
> Sent: Tuesday, May 24, 2011 8:37 AM
> To: Log4NET User
> Subject: Too many log files
> 
>  
> 
> Log4net is ignoring the maximum number of files for my rolling file appender:
> 
>  
> 
>   <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
> 
>     <file value="HeatingModelScheduler.log" />
> 
>     <appendToFile value="true" />
> 
>     <param name="MaxSizeRollBackups" value="5" />
> 
>     <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />
> 
>     <layout type="log4net.Layout.PatternLayout">
> 
>       <conversionPattern value="%date - %message%newline" />
> 
>     </layout>
> 
>   </appender>
> 
>  
> 
> Why do I now have six old log files?
> 
>  
> 
> Thank you very much.
> 
>  
> 
> RobR
> 
>  

RE: Too many log files

Posted by "Lansdaal, Michael T" <mi...@boeing.com>.
Thanks for posting that (I don't follow the mailing list too closely and missed it).  I like the current implementation if your patch that has MaxDateRollBackups of 4 keeping

foo.2010-06-17.log
foo.2010-06-16.log
foo.2010-06-15.log
foo.2010-06-15.1.log
foo.2010-06-15.2.log
foo.2010-06-14.log

I guess the question would be - are the developers interested in folding this into the main branch or are we stuck with making our own patched version?

Thanks, Mike


From: Michael Schall [mailto:mike.schall@gmail.com]
Sent: Tuesday, May 24, 2011 9:57 AM
To: Log4NET User
Subject: Re: Too many log files

I worked on a solution to this that we are using in production...  I posted it on the list without much response...  You are welcome to try this...

http://article.gmane.org/gmane.comp.apache.logging.log4net.devel/885/match=rolling+files+date+time+boundaries+doesn't+support+maximum+number+backup
On Tue, May 24, 2011 at 11:38 AM, Rob Richardson <Ro...@rad-con.com>> wrote:
Thanks for your reply!  I quote a reply to that thread below:

I suspect the difficulties deleting files rolled only by date is that
the appender would need to take into account the DatePattern used when
locating files to be considered for deleting. Counted backups
(log.txt.1, log.txt.2, log.txt.3, etc.) are easier to deal with because
integer sequences are more well-known.

I'm not aware of any efforts to support rolling of files based on
RollingMode.Date.

I would be happy with counted backups, as mentioned in that reply.  How do I set my rolling file appender to do that?

Thanks again!

RobR

________________________________
From: Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com<ma...@boeing.com>]
Sent: Tuesday, May 24, 2011 12:33 PM
To: Log4NET User
Subject: RE: Too many log files

This post (http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F17575%2Fview%2Ftopic%2FDefault.aspx) says that maxSizeRollBackups does not work if you set RollingStyle to RollingMode.Date (and another post I saw says it as "rollingStyle of Date does not support maxSizeRollBackups).

I have implemented my own directory cleaning code in application to clean out log files older than a certain date.

Thanks, Mike

From: Rob Richardson [mailto:Rob.Richardson@rad-con.com<ma...@rad-con.com>]
Sent: Tuesday, May 24, 2011 8:37 AM
To: Log4NET User
Subject: Too many log files

Log4net is ignoring the maximum number of files for my rolling file appender:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="HeatingModelScheduler.log" />
    <appendToFile value="true" />
    <param name="MaxSizeRollBackups" value="5" />
    <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline" />
    </layout>
  </appender>

Why do I now have six old log files?

Thank you very much.

RobR


Re: Too many log files

Posted by Michael Schall <mi...@gmail.com>.
I worked on a solution to this that we are using in production...  I posted
it on the list without much response...  You are welcome to try this...

http://article.gmane.org/gmane.comp.apache.logging.log4net.devel/885/match=rolling+files+date+time+boundaries+doesn't+support+maximum+number+backup

On Tue, May 24, 2011 at 11:38 AM, Rob Richardson <Rob.Richardson@rad-con.com
> wrote:

>  Thanks for your reply!  I quote a reply to that thread below:
>
>
>
> I suspect the difficulties deleting files rolled only by date is that
> the appender would need to take into account the DatePattern used when
> locating files to be considered for deleting. Counted backups
> (log.txt.1, log.txt.2, log.txt.3, etc.) are easier to deal with because
> integer sequences are more well-known.
>
> I'm not aware of any efforts to support rolling of files based on
> RollingMode.Date.
>
>
>
> I would be happy with counted backups, as mentioned in that reply.  How do
> I set my rolling file appender to do that?
>
>
>
> Thanks again!
>
>
>
> RobR
>
>
>  ------------------------------
>
> *From:* Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com]
> *Sent:* Tuesday, May 24, 2011 12:33 PM
> *To:* Log4NET User
> *Subject:* RE: Too many log files
>
>
>
> This post (
> http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F17575%2Fview%2Ftopic%2FDefault.aspx)
> says that maxSizeRollBackups does not work if you set RollingStyle to
> RollingMode.Date (and another post I saw says it as “rollingStyle of Date
> does not support maxSizeRollBackups).
>
>
>
> I have implemented my own directory cleaning code in application to clean
> out log files older than a certain date.
>
>
>
> Thanks, Mike
>
>
>
> *From:* Rob Richardson [mailto:Rob.Richardson@rad-con.com]
> *Sent:* Tuesday, May 24, 2011 8:37 AM
> *To:* Log4NET User
> *Subject:* Too many log files
>
>
>
> Log4net is ignoring the maximum number of files for my rolling file
> appender:
>
>
>
>   <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
>
>     <file value="HeatingModelScheduler.log" />
>
>     <appendToFile value="true" />
>
>     <param name="MaxSizeRollBackups" value="5" />
>
>     <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />
>
>     <layout type="log4net.Layout.PatternLayout">
>
>       <conversionPattern value="%date - %message%newline" />
>
>     </layout>
>
>   </appender>
>
>
>
> Why do I now have six old log files?
>
>
>
> Thank you very much.
>
>
>
> RobR
>

RE: Too many log files

Posted by "Lansdaal, Michael T" <mi...@boeing.com>.
Rob - The Log4net documentation (excellent) has an example of how to do this.  See http://logging.apache.org/log4net/release/config-examples.html and scroll down to the RollingFileAppender section.

Thanks, Mike

From: Rob Richardson [mailto:Rob.Richardson@rad-con.com]
Sent: Tuesday, May 24, 2011 9:38 AM
To: Log4NET User
Subject: RE: Too many log files

Thanks for your reply!  I quote a reply to that thread below:

I suspect the difficulties deleting files rolled only by date is that
the appender would need to take into account the DatePattern used when
locating files to be considered for deleting. Counted backups
(log.txt.1, log.txt.2, log.txt.3, etc.) are easier to deal with because
integer sequences are more well-known.

I'm not aware of any efforts to support rolling of files based on
RollingMode.Date.

I would be happy with counted backups, as mentioned in that reply.  How do I set my rolling file appender to do that?

Thanks again!

RobR

________________________________
From: Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com]
Sent: Tuesday, May 24, 2011 12:33 PM
To: Log4NET User
Subject: RE: Too many log files

This post (http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F17575%2Fview%2Ftopic%2FDefault.aspx) says that maxSizeRollBackups does not work if you set RollingStyle to RollingMode.Date (and another post I saw says it as "rollingStyle of Date does not support maxSizeRollBackups).

I have implemented my own directory cleaning code in application to clean out log files older than a certain date.

Thanks, Mike

From: Rob Richardson [mailto:Rob.Richardson@rad-con.com]
Sent: Tuesday, May 24, 2011 8:37 AM
To: Log4NET User
Subject: Too many log files

Log4net is ignoring the maximum number of files for my rolling file appender:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="HeatingModelScheduler.log" />
    <appendToFile value="true" />
    <param name="MaxSizeRollBackups" value="5" />
    <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline" />
    </layout>
  </appender>

Why do I now have six old log files?

Thank you very much.

RobR

RE: Too many log files

Posted by Rob Richardson <Ro...@rad-con.com>.
Thanks for your reply!  I quote a reply to that thread below:

 

I suspect the difficulties deleting files rolled only by date is that
the appender would need to take into account the DatePattern used when
locating files to be considered for deleting. Counted backups
(log.txt.1, log.txt.2, log.txt.3, etc.) are easier to deal with because
integer sequences are more well-known.

I'm not aware of any efforts to support rolling of files based on
RollingMode.Date.

 

I would be happy with counted backups, as mentioned in that reply.  How
do I set my rolling file appender to do that?

 

Thanks again!

 

RobR

 

________________________________

From: Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com] 
Sent: Tuesday, May 24, 2011 12:33 PM
To: Log4NET User
Subject: RE: Too many log files

 

This post
(http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2F
postid%2F17575%2Fview%2Ftopic%2FDefault.aspx) says that
maxSizeRollBackups does not work if you set RollingStyle to
RollingMode.Date (and another post I saw says it as "rollingStyle of
Date does not support maxSizeRollBackups).

 

I have implemented my own directory cleaning code in application to
clean out log files older than a certain date.

 

Thanks, Mike

 

From: Rob Richardson [mailto:Rob.Richardson@rad-con.com] 
Sent: Tuesday, May 24, 2011 8:37 AM
To: Log4NET User
Subject: Too many log files

 

Log4net is ignoring the maximum number of files for my rolling file
appender:

 

  <appender name="RollingFile"
type="log4net.Appender.RollingFileAppender">

    <file value="HeatingModelScheduler.log" />

    <appendToFile value="true" />

    <param name="MaxSizeRollBackups" value="5" />

    <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="%date - %message%newline" />

    </layout>

  </appender>

 

Why do I now have six old log files?

 

Thank you very much.

 

RobR


RE: Too many log files

Posted by "Lansdaal, Michael T" <mi...@boeing.com>.
This post (http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F17575%2Fview%2Ftopic%2FDefault.aspx) says that maxSizeRollBackups does not work if you set RollingStyle to RollingMode.Date (and another post I saw says it as "rollingStyle of Date does not support maxSizeRollBackups).

I have implemented my own directory cleaning code in application to clean out log files older than a certain date.

Thanks, Mike

From: Rob Richardson [mailto:Rob.Richardson@rad-con.com]
Sent: Tuesday, May 24, 2011 8:37 AM
To: Log4NET User
Subject: Too many log files

Log4net is ignoring the maximum number of files for my rolling file appender:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="HeatingModelScheduler.log" />
    <appendToFile value="true" />
    <param name="MaxSizeRollBackups" value="5" />
    <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline" />
    </layout>
  </appender>

Why do I now have six old log files?

Thank you very much.

RobR

Too many log files

Posted by Rob Richardson <Ro...@rad-con.com>.
Log4net is ignoring the maximum number of files for my rolling file
appender:

 

  <appender name="RollingFile"
type="log4net.Appender.RollingFileAppender">

    <file value="HeatingModelScheduler.log" />

    <appendToFile value="true" />

    <param name="MaxSizeRollBackups" value="5" />

    <param name="DatePattern" value=".yyyy-MM-dd.lo\g" />

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="%date - %message%newline" />

    </layout>

  </appender>

 

Why do I now have six old log files?

 

Thank you very much.

 

RobR