public static void ConfigureLog4Net(string filelogPath, LogLevel displayLogLevel, LogLevel saveLogLevel) { log4net.Repository.Hierarchy.Logger root = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; root.AddAppender(GetConsoleAppender(GetLog4NetLevel(displayLogLevel))); root.AddAppender(GetTraceAppender(GetLog4NetLevel(displayLogLevel))); root.AddAppender(GetSiteFileAppender(filelogPath, GetLog4NetLevel(saveLogLevel))); root.Repository.Configured = true; log4net.Repository.Hierarchy.Logger nhibernate = (log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetLogger("NHibernate").Logger; nhibernate.AddAppender(GetNHibernateFileAppender(filelogPath, GetLog4NetLevel(saveLogLevel))); nhibernate.Additivity = false; nhibernate.Repository.Configured = true; log4net.Repository.Hierarchy.Logger autofac = (log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetLogger("Autofac").Logger; autofac.AddAppender(GetSpringFileAppender(filelogPath, GetLog4NetLevel(saveLogLevel))); autofac.Additivity = false; autofac.Repository.Configured = true; Common.Logging.LogManager.Adapter = new Log4NetLoggerFactoryAdapter(); }
public void AddAppender(string appenderName, ILogger wLogger) { if (!ConfigurationManager.AppSettings.AllKeys.Contains("BackupPath")) { throw new Exception("Parameter 'BackupPath' is mandatory"); } string filename = ConfigurationManager.AppSettings["BackupPath"].ToString() + "\\" + appenderName + ".sql"; log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)wLogger; log4net.Appender.IAppender hasAppender = l.GetAppender(appenderName); if (hasAppender == null) { log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender(); appender.DatePattern = "yyyyMMdd"; appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; appender.AppendToFile = true; appender.File = filename; appender.StaticLogFileName = true; appender.Name = appenderName; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(); layout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; layout.ActivateOptions(); appender.Layout = layout; appender.ActivateOptions(); l.AddAppender(appender); } }
private static void SetUpFileLogger() { string filePattern = "%date %-15.15thread %-5level: %message%newline"; log4net.Layout.ILayout layout; log4net.Layout.PatternLayout normalTextLayout = new log4net.Layout.PatternLayout(filePattern); normalTextLayout.ActivateOptions(); layout = normalTextLayout; log4net.Appender.FileAppender fileAppender = new log4net.Appender.FileAppender(); fileAppender.AppendToFile = false; fileAppender.Encoding = Encoding.UTF8; fileAppender.File = Paths.LogPath; fileAppender.ImmediateFlush = true; fileAppender.Layout = layout; fileAppender.LockingModel = new log4net.Appender.FileAppender.ExclusiveLock(); fileAppender.Name = "File appender"; fileAppender.Threshold = log4net.Core.Level.Debug; fileAppender.ActivateOptions(); // A slight bit of hackery in order to programatically set more than one appender (which isn't supported by BasicConfigurator) log4net.Repository.Hierarchy.Logger root = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; root.AddAppender(fileAppender); root.Repository.Configured = true; }
/// <summary> /// Add a desired appender to the logger /// </summary> /// <param name="loggerName">Name of the logger to which the appender is to be added</param> /// <param name="appender">Appender to add to the logger</param> private 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); }
/// <summary> /// constructor that initialize logger name, file name and log level /// </summary> public CLogger(string sLoggerName, string sFilename, int iLogLevel) { ILog oLogger = LogManager.GetLogger(sLoggerName); string sLogFileName = string.Empty; sLogFileName = GetLogFileName(sFilename); m_oFPLogger = (log4net.Repository.Hierarchy.Logger)oLogger.Logger; log4net.Appender.RollingFileAppender oFPAppender = new RollingFileAppender(); oFPAppender.File = sLogFileName; oFPAppender.MaxSizeRollBackups = 100; oFPAppender.MaximumFileSize = "1MB"; oFPAppender.RollingStyle = RollingFileAppender.RollingMode.Size; oFPAppender.StaticLogFileName = true; oFPAppender.AppendToFile = true; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(); layout.ConversionPattern = "%d %-5p - %m%n%exception"; layout.ActivateOptions(); oFPAppender.Layout = layout; oFPAppender.ActivateOptions(); /** Programmatically configuration will not work it it is not set true**/ m_oFPLogger.Hierarchy.Configured = true; try { m_oFPLogger.AddAppender(oFPAppender); } catch (Exception ex) { throw new Exception("Failed to Add Appender", ex); } m_oFPLogger.Hierarchy.Root.Level = m_oFPLogger.Hierarchy.LevelMap[GetLogLevel(iLogLevel)]; oLogger = null; }
/// <summary> /// Add a desired appender to the logger /// </summary> /// <param name="loggerName">Name of the logger to which the appender is to be added</param> /// <param name="appender">Appender to add to the logger</param> private void AddAppender(log4net.Appender.IAppender appender) { log = log4net.LogManager.GetLogger(_loggerName); log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger; l.AddAppender(appender); }
protected Logger(log4net.Appender.IAppender appender, string logLevel) { log = log4net.LogManager.GetLogger(typeof(T)); log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger; l.Level = l.Hierarchy.LevelMap[logLevel]; l.Additivity = true; l.AddAppender(appender); }
/// <summary> /// Configures the root appender for counting and rolling /// </summary> private void ConfigureRootAppender(FixFlags fixFlags) { log4net.Repository.Hierarchy.Logger root = null; root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root; root.Level = Level.Debug; root.AddAppender(CreateAppender(fixFlags)); root.Repository.Configured = true; }
static log4net.ILog GetLogImpl(string name) { log4net.Core.LogImpl _log = log4net.LogManager.GetLogger(name) as log4net.Core.LogImpl; if (_log != null) { log4net.Repository.Hierarchy.Logger _logimpl = _log.Logger as log4net.Repository.Hierarchy.Logger; if (_logimpl != null) { log4net.Repository.Hierarchy.Logger _logger = (log4net.Repository.Hierarchy.Logger)_logimpl; if (_logger.Appenders.Count == 0) { lock (m_lockObj) { if (_logger.Appenders.Count == 0) { _logger.AddAppender(GetFileAppender(name, log4net.Core.Level.Off, log4net.Core.Level.All)); _logger.AddAppender(m_allAppender); _logger.AddAppender(m_errorAppender); _logger.AddAppender(m_infoAppender); _logger.AddAppender(m_debugAppender); _logger.AddAppender(m_warnAppender); _logger.AddAppender(m_fatalAppender); //log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetAllRepositories()[0]; //hier.GetAppen //log4net.Config.XmlConfigurator.Configure(//.BasicConfigurator.Configure(_appender); } } } } } //缓存 //m_logger.Set(name, _log, m_cachePolicy); return(_log); }
/// <summary> /// Setup the debug log appender and attach it to a logger. /// </summary> /// <param name="logger">The logger you want to attach the event log appender to.</param> private static void SetupDebugLogAppender(log4netLogger logger) { #if DEBUG // Create the debug appender and connect it to our logger. var debugAppender = new DebugAppender(); debugAppender.Layout = FileLogLayout; debugAppender.AddFilter(GetNoAuditFilter()); logger.AddAppender(debugAppender); #endif }
/// <summary> /// A memory appender for logging to memory during startup. Log messages will be re-logged after configuration is loaded. /// </summary> /// <param name="logger"></param> private static void SetupStartupLogAppender(log4netLogger logger) { var startupAppender = new MemoryAppender(); startupAppender.Name = STARTUP_APPENDER_NAME; startupAppender.Layout = LoggerBootstrapper.FileLogLayout; startupAppender.ActivateOptions(); logger.AddAppender(startupAppender); logger.Repository.Configured = true; }
/// <summary> /// Initializes log4net to log to a memory appender which can then be referenced /// </summary> public Logging(log4net.Core.Level level = null) { Logger.Level = level ?? log4net.Core.Level.All; Logger.RemoveAllAppenders(); Logger.AddAppender(MemoryAppender); Logger.Repository.Configured = true; Log.Initialize(new Logger()); }
/// <summary> /// 获取日志设置 /// </summary> public static ILog GetCustomLogger(string loggerName, string loggerSource, string category, bool additivity = false) { return(loggerContainer.GetOrAdd(loggerName + (category ?? "") + loggerSource, delegate(string name) { RollingFileAppender newAppender = null; TWReadParameterAppender appender = null; if (appenderContainer.ContainsKey(loggerName)) { appender = appenderContainer[loggerName]; // 判断日志路径 string filePath = appender.File; if (filePath != null && filePath.EndsWith("\\")) { if (string.IsNullOrEmpty(category)) { filePath = string.Format(@"{0}\{1}\{2}\{3}\{4}\{5}.txt", filePath, loggerName, loggerSource, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.ToString().PadLeft(2, '0')); } else { filePath = string.Format(@"{0}\{1}\{2}\{3}\{4}\{5}\{6}.txt", filePath, loggerName, category, loggerSource, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.ToString().PadLeft(2, '0')); } } newAppender = GetNewFileApender(loggerName, filePath, appender.MaxSizeRollBackups, appender.AppendToFile, true, appender.MaximumFileSize, RollingFileAppender.RollingMode.Composite, appender.DatePattern, appender.LayoutPattern); } else { newAppender = GetNewFileApender(loggerName, GetFile(category, loggerName), MAX_SIZE_ROLL_BACKUPS, true, true, MAXIMUM_FILE_SIZE, RollingFileAppender.RollingMode.Composite, DATE_PATTERN, LAYOUT_PATTERN); } log4net.Repository.Hierarchy.Hierarchy repository = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(); log4net.Repository.Hierarchy.Logger logger = repository.LoggerFactory.CreateLogger(repository, loggerName); logger.Hierarchy = repository; logger.Parent = repository.Root; logger.Level = GetLoggerLevel(appender == null ? LEVEL : appender.Level); logger.Additivity = additivity; logger.AddAppender(newAppender); logger.Repository.Configured = true; return new LogImpl(logger); })); }
/// <summary> /// This method creates the loggers /// </summary> /// <param name="fileAppender"></param> /// <param name="loggerName"></param> private void CreateLogger(log4net.Appender.FileAppender fileAppender, string loggerName) { log4net.Repository.Hierarchy.Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); log4net.Repository.Hierarchy.Logger logger = (log4net.Repository.Hierarchy.Logger)hierarchy.GetLogger(loggerName); if (logger.GetAppender(fileAppender.Name) == null) { logger.AddAppender(fileAppender); } hierarchy.Configured = true; }
/// <summary> /// Setup the console log appender and attach it to a logger. /// </summary> /// <param name="logger">The logger you want to attach the console log appender to.</param> /// <param name="config">The configuration for the appender.</param> private static void SetupConsoleLogAppender(log4netLogger logger) { var appender = new ConsoleAppender(); appender.Name = ConsoleLogAppenderName; appender.Layout = FileLogLayout; appender.Threshold = Level.Warn; appender.AddFilter(GetNoAuditFilter()); appender.ActivateOptions(); logger.AddAppender(appender); }
public static void AddAppender2(ILog log, IAppender appender) { log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger; if (l.GetAppender(appender.Name) != null) { return; } l.Additivity = false; l.AddAppender(appender); }
/// <summary> /// Turns on all logging for the system. /// </summary> private void TurnOnLogging() { log4net.Repository.ILoggerRepository[] repositories = log4net.LogManager.GetAllRepositories(); IList <IAppender> toRemove; // Configure all loggers to be at the debug level. foreach (log4net.Repository.ILoggerRepository repository in repositories) { repository.Threshold = repository.LevelMap["DEBUG"]; log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)repository; log4net.Core.ILogger[] loggers = hier.GetCurrentLoggers(); foreach (log4net.Core.ILogger logger in loggers) { log4net.Repository.Hierarchy.Logger log = (log4net.Repository.Hierarchy.Logger)logger; log.Level = hier.LevelMap["DEBUG"]; log.AddAppender(new NullAppender()); toRemove = new List <IAppender>(); foreach (var appender in log.Appenders) { if (appender.GetType() != typeof(NullAppender)) { toRemove.Add(appender); } } foreach (IAppender appender in toRemove) { log.RemoveAppender(appender); } } } // Configure the root logger. log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); log4net.Repository.Hierarchy.Logger rootLogger = h.Root; rootLogger.Level = h.LevelMap["DEBUG"]; rootLogger.AddAppender(new NullAppender()); toRemove = new List <IAppender>(); foreach (var appender in rootLogger.Appenders) { if (appender.GetType() != typeof(NullAppender)) { toRemove.Add(appender); } } foreach (IAppender appender in toRemove) { rootLogger.RemoveAppender(appender); } }
/// <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 }
/// <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 }
/// <summary> /// Setup the file log appender and attach it to a logger. /// </summary> /// <param name="logger">The logger you want to attach the file appender to.</param> /// <param name="config">The configuration for the appender.</param> /// <exception cref="System.Exception">If an exception occurs, the Event Log Appender is setup /// to handle output.</exception> private static void SetupFileLogAppender(log4netLogger logger, ILogConfig config) { string logFileName = config.GetFullLogFileName(); try { var appender = SetupRollingFileAppender(config, logFileName, "FileLog", FileLogLayout); appender.AddFilter(GetNoAuditFilter()); appender.ActivateOptions(); logger.AddAppender(appender); } catch (Exception) { // Fallback to the event logger if we cannot setup a file logger. SetupEventLogAppender(logger, config); } }
private FileLogger() { LoggerInterface = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log4net.Repository.Hierarchy.Logger logger = (log4net.Repository.Hierarchy.Logger)LoggerInterface.Logger; logger.Level = Level.All; string logDirectory = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + "Logs"; if (!System.IO.Directory.Exists(logDirectory)) { System.IO.Directory.CreateDirectory(logDirectory); } string logFileName = "KidsNoteForEveryone.log"; string logFilePathName = logDirectory + System.IO.Path.DirectorySeparatorChar + logFileName; logger.AddAppender(CreateRollingFileAppender(logFileName, logFilePathName)); }
/// <summary> /// Setup the audit log file appender and attach it to a logger. /// </summary> /// <param name="logger">The logger you want to attach the audit log appender to.</param> /// <param name="config">The configuration for the appender.</param> private static void SetupAuditLogger(log4netLogger logger, ILogConfig config) { if (!config.IsAuditLogEnabled) { return; } string logFileName = config.GetFullLogFileName().Replace(".log", "_audit.log"); try { var appender = SetupRollingFileAppender(config, logFileName, AuditLogAppenderName, AuditLogLayout); appender.AddFilter(GetAuditFilter()); appender.AddFilter(new DenyAllFilter()); appender.ActivateOptions(); logger.AddAppender(appender); } catch (Exception) { } }
//LOGGER /// <summary> /// Static function that configures the log4net logger object. /// </summary> /// <returns>Interface to the logger.</returns> static log4net.ILog ConfigureLogger() { // Programmatic configuration // follows (with some streamlining) the example from Brendan Long and Ron Grabowski // org.apache.logging.log4net-user // These config statements create a RollingFile Appender. Rolling File Appenders rollover on each execution of the test harness, // in this case, following the Composite RollingMode. Alternative log4net appenders may be added or replace this default appender at the programmer's discretion. // PatternLayout layout = new PatternLayout("%d [%t] %-5p %c - %m%n"); PatternLayout layout = new PatternLayout("%d %-5p %c - %m%n"); log4net.Appender.RollingFileAppender appender = new RollingFileAppender(); appender.Layout = layout; appender.AppendToFile = true; appender.MaxFileSize = 10000000; appender.RollingStyle = RollingFileAppender.RollingMode.Composite; appender.StaticLogFileName = true; appender.File = Properties.Settings.Default.log4netDir; // all logs will be created in the subdirectory logs below where the test harness is executing // Configure filter to accept log messages of any level. log4net.Filter.LevelMatchFilter traceFilter = new log4net.Filter.LevelMatchFilter(); traceFilter.LevelToMatch = log4net.Core.Level.Debug; appender.ClearFilters(); appender.AddFilter(traceFilter); appender.ImmediateFlush = true; appender.ActivateOptions(); // Attach appender into hierarchy log4net.Repository.Hierarchy.Logger root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root; root.AddAppender(appender); root.Repository.Configured = true; log4net.ILog log = log4net.LogManager.GetLogger("WBM-LOGGER"); return(log); }
private static void SetUpConsoleLogger() { string consolePattern = "%message%newline"; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(consolePattern); layout.Header = ""; layout.Footer = ""; layout.ActivateOptions(); log4net.Appender.ConsoleAppender consoleAppender = new log4net.Appender.ConsoleAppender(); //consoleAppender.ErrorHandler = new LoggingErrorHandler(); consoleAppender.Name = "Console appender"; consoleAppender.Threshold = log4net.Core.Level.Warn; consoleAppender.Layout = layout; consoleAppender.Target = "Console.Out"; consoleAppender.ActivateOptions(); // A slight bit of hackery in order to programatically set more than one appender (which isn't supported by BasicConfigurator) log4net.Repository.Hierarchy.Logger root = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; root.AddAppender(consoleAppender); root.Repository.Configured = true; }
public static XMightLogger GerLoggerForName(String pName, String pHeader = null) { ILog logger = log4net.LogManager.GetLogger(pName); log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)logger.Logger; l.RemoveAllAppenders(); DateFolderRollingFileAppender app1 = new DateFolderRollingFileAppender(); app1.Name = pName + "Appender"; app1.File = "logs\\" + pName; app1.PreserveLogFileNameExtension = true; app1.DatePattern = ".dd-MM-yyyy"; app1.AppendToFile = false; app1.ImmediateFlush = true; app1.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; app1.StaticLogFileName = false; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(); layout.ConversionPattern = "%d{dd-MM-yyyy HH:mm:ss.fff}\t%timestamp\t%message%newline"; if (!String.IsNullOrEmpty(pHeader)) { layout.Header = pHeader + "\n"; } layout.ActivateOptions(); app1.Layout = layout; app1.ActivateOptions(); l.AddAppender(app1); l.Additivity = false; l.Level = log4net.Core.Level.Debug; return(new XMightLogger(logger)); }