Exemple #1
0
        private static IAppender GetEventLogAppender()
        {
            var appender = new EventLogAppender
            {
                Threshold = Level.Error,
                Layout    = GetPattermLayout()
            };

            var mappers = new[]
            {
                new EventLogAppender.Level2EventLogEntryType
                {
                    EventLogEntryType = EventLogEntryType.Error,
                    Level             = Level.Error
                },
                new EventLogAppender.Level2EventLogEntryType
                {
                    EventLogEntryType = EventLogEntryType.Warning,
                    Level             = Level.Warn
                },
                new EventLogAppender.Level2EventLogEntryType
                {
                    EventLogEntryType = EventLogEntryType.Error,
                    Level             = Level.Fatal
                }
            };

            foreach (var mapper in mappers)
            {
                appender.AddMapping(mapper);
            }
            appender.ActivateOptions();
            return(appender);
        }
Exemple #2
0
        public static void Configure()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            hierarchy.Root.RemoveAllAppenders();
            PatternLayout defaultPatternLayout = new PatternLayout();

            defaultPatternLayout.ConversionPattern = DefaultLogPattern;
            defaultPatternLayout.ActivateOptions();
            RollingFileAppender rollingFileAppender = GetRollingFileAppender(defaultPatternLayout);

            hierarchy.Root.AddAppender(rollingFileAppender);
            EventLogAppender eventLogAppender = GetEventLogAppender(defaultPatternLayout);

            hierarchy.Root.AddAppender(eventLogAppender);
            OutputDebugStringAppender outputDebugStringAppender = GetOutputDebugStringAppender(defaultPatternLayout);

            hierarchy.Root.AddAppender(outputDebugStringAppender);
            PatternLayout consolePatternLayout = new PatternLayout();

            consolePatternLayout.ConversionPattern = ConsoleLogPattern;
            consolePatternLayout.ActivateOptions();
            ColoredConsoleAppender coloredConsoleAppender = GetColoredConsoleAppender(consolePatternLayout);

            hierarchy.Root.AddAppender(coloredConsoleAppender);
            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }
Exemple #3
0
        public void TestGetEntryType()
        {
            EventLogAppender eventAppender = new EventLogAppender();

            eventAppender.ActivateOptions();

            Assert.AreEqual(
                EventLogEntryType.Information,
                GetEntryType(eventAppender, Level.All));

            Assert.AreEqual(
                EventLogEntryType.Information,
                GetEntryType(eventAppender, Level.Debug));

            Assert.AreEqual(
                EventLogEntryType.Information,
                GetEntryType(eventAppender, Level.Info));

            Assert.AreEqual(
                EventLogEntryType.Warning,
                GetEntryType(eventAppender, Level.Warn));

            Assert.AreEqual(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Error));

            Assert.AreEqual(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Fatal));

            Assert.AreEqual(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Off));
        }
        public static void InitLogging()
        {
            var layout = new PatternLayout {
                ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
            };

            layout.ActivateOptions();

            var filter = new LevelRangeFilter {
                LevelMin = Level.Warn, LevelMax = Level.Fatal
            };

            filter.ActivateOptions();

            var eventLogAppender = new EventLogAppender
            {
                ApplicationName = "NServiceBus Sample Server",
                Threshold       = Level.All,
                Layout          = layout
            };

            eventLogAppender.AddFilter(filter);
            eventLogAppender.ActivateOptions();

            var consoleAppender = new ConsoleAppender
            {
                Threshold = Level.Info,
                Layout    = layout
            };

            BasicConfigurator.Configure(consoleAppender);

            LogManager.Use <Log4NetFactory>();
        }
        public void TestGetEntryTypeForExistingApplicationName()
        {
            var eventAppender = new EventLogAppender {
                ApplicationName = "Winlogon"
            };

            eventAppender.ActivateOptions();

            Assert.Equal(EventLogEntryType.Information,
                         GetEntryType(eventAppender, Level.All));

            Assert.Equal(
                EventLogEntryType.Information,
                GetEntryType(eventAppender, Level.Debug));

            Assert.Equal(
                EventLogEntryType.Information,
                GetEntryType(eventAppender, Level.Info));

            Assert.Equal(
                EventLogEntryType.Warning,
                GetEntryType(eventAppender, Level.Warn));

            Assert.Equal(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Error));

            Assert.Equal(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Fatal));

            Assert.Equal(
                EventLogEntryType.Error,
                GetEntryType(eventAppender, Level.Off));
        }
        public void ActivateOptionsDisablesAppenderIfSourceDoesntExist()
        {
            EventLogAppender eventAppender = new EventLogAppender();

            eventAppender.ActivateOptions();
            Assert.AreEqual(Level.Off, eventAppender.Threshold);
        }
Exemple #7
0
            private static IAppender CreateEventLogAppender(string logName)
            {
                EventLogAppender appender = new EventLogAppender();

                appender.Layout          = CreateOnlyMessageLayout();
                appender.ApplicationName = "AlarmWorkflow/" + logName;

                return(appender);
            }
Exemple #8
0
        private static void SetupLogger(string eventSource)
        {
            var hierarchy = (Hierarchy)LogManager.GetRepository();

            var patternLayout = new PatternLayout
            {
                ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
            };

            patternLayout.ActivateOptions();

            if (Environment.UserInteractive)
            {
                var coloredConsoleAppender = new ColoredConsoleAppender
                {
                    Layout = patternLayout
                };
                coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors
                {
                    Level     = Level.Debug,
                    ForeColor = ColoredConsoleAppender.Colors.Green
                });
                coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors
                {
                    Level     = Level.Info,
                    ForeColor = ColoredConsoleAppender.Colors.White
                });
                coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors
                {
                    Level     = Level.Warn,
                    ForeColor = ColoredConsoleAppender.Colors.Yellow
                });
                coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors
                {
                    Level     = Level.Error,
                    ForeColor = ColoredConsoleAppender.Colors.White,
                    BackColor = ColoredConsoleAppender.Colors.Red
                });
                coloredConsoleAppender.ActivateOptions();
                hierarchy.Root.AddAppender(coloredConsoleAppender);
            }
            else
            {
                var eventLogAppender = new EventLogAppender
                {
                    ApplicationName = eventSource,
                    Layout          = patternLayout
                };
                eventLogAppender.ActivateOptions();
                hierarchy.Root.AddAppender(eventLogAppender);
            }

            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }
Exemple #9
0
        private static EventLogAppender GetEventLogAppender(PatternLayout patternLayout)
        {
            EventLogAppender eventLogAppender = new EventLogAppender();

            eventLogAppender.LogName         = EventLogName;
            eventLogAppender.ApplicationName = ApplicationName;
            eventLogAppender.Layout          = patternLayout;
            eventLogAppender.Threshold       = Level.Warn;
            eventLogAppender.ActivateOptions();
            return(eventLogAppender);
        }
Exemple #10
0
        public void SetUp()
        {
            XmlDocument xmlDoc      = new XmlDocument();
            XmlElement  log4NetNode = xmlDoc.CreateElement(Log4NetXmlConstants.Log4Net);

            IElementConfiguration configuration = Substitute.For <IElementConfiguration>();

            configuration.ConfigXml.Returns(xmlDoc);
            configuration.Log4NetNode.Returns(log4NetNode);

            mSut = new EventLogAppender(configuration);
        }
        public EventLogAppender CreateEventLogAppender(Level threshold)
        {
            var tracer        = new EventLogAppender();
            var patternLayout = CreatePatternLayout(FILE_LOG_PATTERN);

            tracer.Layout          = patternLayout;
            tracer.Name            = "EventLogAppender";
            tracer.Threshold       = threshold;
            tracer.ApplicationName = _applicationName;
            tracer.ActivateOptions();
            return(tracer);
        }
Exemple #12
0
        public override IAppender GetAppender()
        {
            var appender = new EventLogAppender
            {
                Threshold       = Level.All,
                Layout          = new PatternLayout(layoutString),
                ApplicationName = ApplicationName,
            };

            appender.ActivateOptions();
            return(appender);
        }
        public void SetUp()
        {
            EventLogAppender eventLogAppender = new EventLogAppender();

            eventLogAppender.LogName         = s_eventLogName;
            eventLogAppender.ApplicationName = s_eventLogSource;
            eventLogAppender.SecurityContext = NullSecurityContext.Instance;
            eventLogAppender.Layout          = new PatternLayout("%m\r\n\r\n");
            BasicConfigurator.Configure(eventLogAppender);

            _logger = LogManager.GetLogger("The Name").Logger;
            _log    = new Log4NetLog(_logger);
            _testEventLog.Clear();
        }
        /// <summary>
        /// Setup the event log appender and attach it to a logger.
        /// </summary>
        /// <param name="logger">The logger you want to attach the event log appender to.</param>
        /// <param name="config">The configuration for the appender.</param>
        private static void SetupEventLogAppender(log4netLogger logger, ILogConfig config)
        {
#if NET45
            var appender = new EventLogAppender();
            appender.Layout          = eventLoggerLayout;
            appender.Name            = EventLogAppenderName;
            appender.LogName         = EventLogName;
            appender.ApplicationName = EventLogSourceName;
            appender.AddFilter(GetNoAuditFilter());
            appender.ActivateOptions();

            logger.AddAppender(appender);
#endif
        }
        internal static EventLogAppender GetEventLogAppender(string logName, string applicationName)
        {
            var layout = new PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline");

            layout.ActivateOptions();
            var appender = new EventLogAppender
            {
                ApplicationName = applicationName,
                LogName         = logName,
                Layout          = layout,
            };

            appender.ActivateOptions();
            return(appender);
        }
Exemple #16
0
        public static void LoadEventLogAppender()
        {
            EventLogAppender appender = new EventLogAppender();

            appender.Name = "EventLogAppender";

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = _ConversionPattern;
            patternLayout.ActivateOptions();
            appender.Layout = patternLayout;

            appender.ActivateOptions();
            BasicConfigurator.Configure(appender);
        }
        /// <summary>
        /// A temporary event log appender for logging during startup (before config is loaded)
        /// </summary>
        /// <param name="logger"></param>
        private static void SetupTemporaryEventLogAppender(log4netLogger logger)
        {
#if NET45
            var appender = new EventLogAppender();
            appender.Layout          = eventLoggerLayout;
            appender.Name            = TemporaryEventLogAppenderName;
            appender.LogName         = EventLogName;
            appender.ApplicationName = EventLogSourceName;
            appender.Threshold       = Level.Warn;
            appender.AddFilter(GetNoAuditFilter());
            appender.ActivateOptions();

            logger.AddAppender(appender);
#endif
        }
Exemple #18
0
        // Configure the event log logger programmatically.
        public static void ConfigureEventLogAppender(ILog log, string sourceName, Level level)
        {
            bool isConfigured = log.Logger.Repository.GetAppenders().OfType <EventLogAppender>().Any();

            if (!isConfigured)
            {
                // Setup RollingFileAppender
                EventLogAppender appender = new EventLogAppender();
                appender.Layout          = new log4net.Layout.PatternLayout(@"[%thread] - %message%newline");
                appender.ApplicationName = sourceName;
                appender.Name            = "EventLogAppender";
                appender.Threshold       = level;
                appender.ActivateOptions();
                BasicConfigurator.Configure(appender);
            }
        }
        public static ILog CreateAuditTrail(string appName, string logSection = "CustomEventLog",
                                            string loggerName = null, [CallerMemberName] string caller = null)
        {
            var eventLogAppender = new EventLogAppender()
            {
                ApplicationName = appName ?? "[SkyNetCloud module]",
                LogName         = logSection,
            };
            var pl = new PatternLayout()
            {
                ConversionPattern = _conversionPattern,
            };

            pl.ActivateOptions();
            eventLogAppender.Layout = pl;
            eventLogAppender.ActivateOptions();
            LogManager.ResetConfiguration();
            log4net.Config.BasicConfigurator.Configure(eventLogAppender);
            return(LogManager.GetLogger(loggerName ?? caller));
        }
        public void Integration()
        {
            var rep           = LogManager.CreateRepository(Guid.NewGuid().ToString());
            var eventAppender = new EventLogAppender
            {
                ApplicationName = "xUnit",
                Layout          = new SimpleLayout()
            };

            eventAppender.ActivateOptions();

            var stringAppender = new OutputDebugStringAppender
            {
                Layout = new SimpleLayout()
            };

            stringAppender.ActivateOptions();
            BasicConfigurator.Configure(rep, eventAppender, stringAppender);

            var logger = LogManager.GetLogger(rep.Name, "LoggerThread");

            logger.Error(new Exception("exception test"));
        }
Exemple #21
0
        //
        // Helper functions to dig into the appender
        //

        private static EventLogEntryType GetEntryType(EventLogAppender appender, Level level)
        {
            return((EventLogEntryType)Utils.InvokeMethod(appender, "GetEntryType", level));
        }
        public static void Configure(ServiceConfiguration configuration, string serviceName)
        {
            var hierarchy = (Hierarchy)LogManager.GetRepository();

            hierarchy.Root.RemoveAllAppenders();

            // see http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

            var layout = new PatternLayout
            {
                ConversionPattern = "%date{dd-MMM-yyyy HH:mm:ss}  %-5level  %message%n%exception%n"
            };

            Level level;

            switch (configuration.LogThreshold)
            {
            case LogLevel.Info:
                level = Level.Info;
                break;

            case LogLevel.Debug:
                level = Level.Debug;
                break;

            case LogLevel.Warn:
                level = Level.Warn;
                break;

            case LogLevel.Error:
                level = Level.Error;
                break;

            default:
                throw new Exception("Unhandled log level");
            }

            //var fileAppender = new RollingFileAppender
            //{
            //    Layout = layout,
            //    AppendToFile = true,
            //    RollingStyle = RollingFileAppender.RollingMode.Composite,
            //    DatePattern = "yyyy-MM-dd",
            //    File = Path.Combine(configuration.ImporterLogFolder ?? String.Empty, "NBNAutoImportlog"),
            //    LockingModel = new FileAppender.MinimalLock(),
            //    MaxFileSize = 2097152, //2Mb
            //    CountDirection = 1,
            //    StaticLogFileName = false,
            //    //File = settings.LogDirectory
            //    Threshold = level
            //};

            var eventLogAppender = new EventLogAppender()
            {
                ApplicationName = serviceName,
                Layout          = layout,
                Threshold       = level
            };

            var consoleAppender = new ConsoleAppender
            {
                Layout = layout,
            };

            layout.ActivateOptions();

            eventLogAppender.ActivateOptions();
            log4net.Config.BasicConfigurator.Configure(eventLogAppender);

            //fileAppender.ActivateOptions();
            //log4net.Config.BasicConfigurator.Configure(fileAppender);

            consoleAppender.ActivateOptions();
            log4net.Config.BasicConfigurator.Configure(consoleAppender);
        }
        //
        // Helper functions to dig into the appender
        //

        private static System.Diagnostics.EventLogEntryType GetEntryType(EventLogAppender appender, Level level)
        {
            return((System.Diagnostics.EventLogEntryType)Utils.InvokeMethod(appender, "GetEntryType", level));
        }