1

Is there any way to specify the size of the event viewer log using EventLogAppender?

Or do I need to grab the logger using the C#/Windows Api's?

By default it's set to 1028KB but I need it to be 10MB.

Gonçalo Cardoso
  • 2,253
  • 4
  • 34
  • 63
  • Can you explain what exactly you want to be 10 MB? The event log [can only hold entries of ~32 KB anyway](http://stackoverflow.com/questions/13418312/nt-eventlog-single-message-size). – CodeCaster May 27 '15 at 10:32
  • 1
    It's not the individual message that I want to be 10MB,. it's the all log. – Gonçalo Cardoso May 27 '15 at 10:39
  • https://technet.microsoft.com/en-us/library/cc748849.aspx – CodeCaster May 27 '15 at 10:40
  • I need it to be done programmatically, preferably using Log4Net EventLogAppender properties. Currently I'm making it by using https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.maximumkilobytes%28v=vs.110%29.aspx – Gonçalo Cardoso May 27 '15 at 10:47

1 Answers1

0

Solved it by using this:

public static void setEventLogAppenderMaximumSize(log4net.ILog aLogger)
{
    log4net.Appender.IAppender[] logAppenders = aLogger.Logger.Repository.GetAppenders();
    if (logAppenders != null && logAppenders.Length > 0)
    {
        string logName = ((log4net.Appender.EventLogAppender)logAppenders[0]).LogName;

        EventLog[] eventLogs = EventLog.GetEventLogs();
        foreach (EventLog e in eventLogs)
        {
            if (e.Log == logName)
            {
                int newLogSizeInKB = 102400;    //10MB

                if (e.MaximumKilobytes < newLogSizeInKB)
                {
                    e.MaximumKilobytes = newLogSizeInKB;
                }
                return;
            }
        }
    }
}
Gonçalo Cardoso
  • 2,253
  • 4
  • 34
  • 63