logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Macarthur Work <macarthur.w...@gmail.com>
Subject Problem with eventlog and source ?
Date Wed, 15 Sep 2010 15:03:44 GMT
I'm using log4net without config file.

When I want add log by System.Diagnostics.EventLog everything goes ok:


    if (!EventLog.SourceExists("TestSource"))
                {
                    EventLog.CreateEventSource("TestSource", TestLog");
                }

    System.Diagnostics.EventLog.WriteEntry("TestSource", "someMessage",
eventType);

But the same action when I using log4net doesn't work:



     var log4net = new Log4NetEventLogger("TestSource");
                log4net.AddEntry("TestSource", "TestLog", new Exception(),
"additionalMesage", System.Diagnostics.EventLogEntryType.Error);




    public class MyLog4NetEventLogger
        {
            #region Declaration

            private readonly string _loggerName;
            private ILog _logger;
            private bool _clientHasOwnConfiguration;

            #endregion Declarations

            #region constructor
            public MyLog4NetEventLogger(string logerName)
            {
                _loggerName = logerName;

                _logger = LogManager.GetLogger(_loggerName);

               if (_logger.Logger.Repository.GetAppenders().Where(a =>
a.Name == "EventLogAppender").Count() == 0)
                    {
                        log4net.Config.BasicConfigurator.Configure();
                        AddAppender(logerName, CreateEventLogAppender());

                    }

            }
            #endregion constructor
            private static void AddAppender(string loggerName, IAppender
appender)
            {
                ILog log = LogManager.GetLogger(loggerName);
                var l = (log4net.Repository.Hierarchy.Logger)log.Logger;

                l.AddAppender(appender);
            }


            public void AddEntry(string sourceApplicationName, string
logName, Exception ex, string additionalMessage, EventLogEntryType
eventType)
            {
                SetEventLogAppender(sourceApplicationName, logName);

                if (_logger.Logger.Repository.GetAppenders().Where(a =>
a.Name == "EventLogAppender").Count() == 0)
                {
                    throw new NotSupportedException();
                }


                var smth = _logger.Logger.Repository.GetAppenders();
                switch (eventType)
                {
                    case EventLogEntryType.Error:
                        _logger.Error(additionalMessage, ex);
                        break;
                    case EventLogEntryType.Warning:
                        _logger.Warn(additionalMessage, ex);
                        break;
                    case EventLogEntryType.Information:
                        _logger.Info(additionalMessage, ex);
                        break;
                    case EventLogEntryType.SuccessAudit:
                    case EventLogEntryType.FailureAudit:
                        throw new NotImplementedException();
                    default:
                        throw new ArgumentOutOfRangeException("eventType");
                }
            }

            private static void SetEventLogAppender(string applicationName,
string logName)
            {
                if (!EventLog.SourceExists(applicationName))
                {
                    EventLog.CreateEventSource(applicationName, logName);
                }

                var eventLogAppender =
                      LogManager.GetRepository().GetAppenders()
                          .Where(appender => appender.Name ==

"EventLogAppender").FirstOrDefault() as EventLogAppender;

                if (eventLogAppender != null)
                {

                    eventLogAppender.LogName = logName;
                    eventLogAppender.ApplicationName = applicationName;
                    eventLogAppender.ActivateOptions();
                }
            }

            private static IAppender CreateEventLogAppender()
            {

                var appender = new EventLogAppender { Name =
"EventLogAppender", ApplicationName = "CMNLibrary" };


                var layout = new log4net.Layout.PatternLayout
                {
                    ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
                };

                layout.ActivateOptions();


                var filter = new log4net.Filter.LoggerMatchFilter {
AcceptOnMatch = true };
                var filterDeny = new log4net.Filter.DenyAllFilter();
                var filterErrorType = new log4net.Filter.LevelRangeFilter {
AcceptOnMatch = true, LevelMin = Level.Error };

                appender.AddFilter(filter);
                appender.AddFilter(filterDeny);
                appender.AddFilter(filterErrorType);

                appender.ActivateOptions();

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



                return appender;
            }
        }

Thanks & Regards,

Mime
View raw message