예제 #1
0
        static ConfigLogger()
        {
            Instance = new ConfigLogger();
            var section = (LoggingSection) ConfigurationManager.GetSection("BitFactory.Logging");

            if (section.IsConfiguredForThisMachine())
            {
                Instance.Application = section.Application != ""
                    ? section.Application
                    : AppDomain.CurrentDomain.FriendlyName;

                Instance.Formatter = new LogEntryFormatStringFormatter(section.FormatString, Instance.OnLoggingError);
                Configure(Instance, Instance, section);
            }
        }
예제 #2
0
        private static Logger Configure(CompositeLogger aLogger, ConfigLogger aConfigLogger, CompositeLoggerElement aCompLoggerElement)
        {
            Configure((Logger)aLogger, aConfigLogger, aCompLoggerElement);

            foreach (CompositeLoggerElement element in aCompLoggerElement.CompositeLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = new CompositeLogger();
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));
                }

            foreach (FileLoggerElement element in aCompLoggerElement.FileLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = new FileLogger(element.FileName);
                    logger.FileName = element.FileName;
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));//, section));
                }

            foreach (RollingDateFileLoggerElement element in aCompLoggerElement.RollingDateFileLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = RollingFileLogger.NewRollingDateFileLogger(element.FormattedFileName);
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));//, section));
                }

            foreach (RollingSizeFileLoggerElement element in aCompLoggerElement.RollingSizeFileLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = RollingFileLogger.NewRollingSizeFileLogger(element.FileName, element.MaxSize);
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));//, section));
                }

            foreach (EmailLoggerElement element in aCompLoggerElement.EmailLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = new EmailLogger(new System.Net.Mail.SmtpClient(element.SmtpHost), element.From, element.To, element.Subject);
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));//, section));
                }

            foreach (SocketLoggerElement element in aCompLoggerElement.SocketLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = new SerialSocketLogger(element.Host, element.Port);
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));//, section));
                }

            foreach(ConsoleLoggerElement element in aCompLoggerElement.ConsoleLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = TextWriterLogger.NewConsoleLogger();
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));
                }

            foreach(EventLogLoggerElement element in aCompLoggerElement.EventLogLoggers)
                if (element.IsConfiguredForThisMachine())
                {
                    var logger = new EventLogLogger(new System.Diagnostics.EventLog(element.LogName, Environment.MachineName, aConfigLogger.Application));
                    aLogger.AddLogger(element.Name, Configure(logger, Instance, element));
                }

            return aLogger;
        }
예제 #3
0
        private static Logger Configure(Logger aLogger, ConfigLogger aConfigLogger, LoggerElement aLoggerElement)
        {
            aLogger.Application = aConfigLogger.Application;

            var includeCategories = aLoggerElement.IncludeCategories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var excludeCategories = aLoggerElement.ExcludeCategories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            LogEntryFilter filter = null;

            if (includeCategories.Length > 0)
            {
                filter = new LogEntryCategoryFilter(true);
                foreach (string cat in includeCategories)
                    ((LogEntryCategoryFilter)filter).AddCategory(cat);
            }
            else if (excludeCategories.Length > 0)
            {
                filter = new LogEntryCategoryFilter(false);
                foreach (string cat in excludeCategories)
                    ((LogEntryCategoryFilter)filter).AddCategory(cat);
            }
            else
            {
                filter = new LogEntryPassFilter();
            }

            aLogger.Filter = filter;
            aLogger.SeverityThreshold = aLoggerElement.Severity;
            aLogger.Formatter = new LogEntryFormatStringFormatter((aLoggerElement.FormatString != "" ? aLoggerElement.FormatString : ((LogEntryFormatStringFormatter)aConfigLogger.Formatter).FormatString), aLogger.OnLoggingError);

            aLogger.Enabled = aLoggerElement.IsEnabled;

            var resultingLogger = aLoggerElement.IsInsistent
                ? new InsistentLogger(aLogger, 100, 180)
                : aLogger;

            resultingLogger = aLoggerElement.IsAsynchronous
                ? new AsyncLogger(resultingLogger)
                : resultingLogger;

            resultingLogger.Enabled = aLoggerElement.IsEnabled;

            return resultingLogger;
        }