public void Close() { SetLevel("OFF"); log4net.Core.IAppenderAttachable closingAppenders = (log4net.Core.IAppenderAttachable)log.Logger; AppenderCollection collection = closingAppenders.Appenders; for (int i = 0; i < collection.Count; i++) { if (collection[i] is BufferingForwardingAppender) { //This FLUSH and close the current appenders along with all of its children appenders ((BufferingForwardingAppender)collection[i]).Close(); } } this.RemoveAllAppender(log); }
static void CreateAppender(AppenderConfiguration configuration, log4net.Core.IAppenderAttachable collection) { if (configuration != null) { if (!string.IsNullOrEmpty(configuration.Name)) { if (collection.GetAppender(configuration.Name) == null) { // Create the appender and add it to the loggers collection of appenders. // Create the appender based on the type of configuration. if (configuration is RollingFileAppenderConfiguration) { var rolling_file_configuration = configuration as RollingFileAppenderConfiguration; // If a file name is not provided for the log file then attempt to use the applications name. var rolling_appender = new log4net.Appender.RollingFileAppender { Name = configuration.Name, MaxSizeRollBackups = Convert.ToInt32(rolling_file_configuration.MaxArchivedFiles), MaximumFileSize = rolling_file_configuration.RollSizeKB, RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size, StaticLogFileName = true, LockingModel = new log4net.Appender.RollingFileAppender.MinimalLock(), File = rolling_file_configuration.Path + (string.IsNullOrEmpty(rolling_file_configuration.FileName) ? GetApplicationName() + ".log" : rolling_file_configuration.FileName), PreserveLogFileNameExtension = true, Layout = new log4net.Layout.PatternLayout(rolling_file_configuration.Template), }; //rolling_appender.AddFilter(new log4net.Filter.PropertyFilter{ Key="", StringToMatch=""}); //.StringMatchFilter { StringToMatch = category }); //rolling_appender.AddFilter(new log4net.Filter.DenyAllFilter()); rolling_appender.ActivateOptions(); collection.AddAppender(rolling_appender); } else { throw new Exception("Unable to create the appender. " + configuration.GetType().Name + "'s are not supported."); } } } else { throw new Exception("Unable to create the appender. The name property is undefined."); } } }
public void Close() { try { log4net.Core.IAppenderAttachable closingAppenders = (log4net.Core.IAppenderAttachable)_log.Logger; AppenderCollection collection = closingAppenders.Appenders; for (int i = 0; i < collection.Count; i++) { if (collection[i] is BufferingForwardingAppender) { //This FLUSH and close the current appenders along with all of its children appenders ((BufferingForwardingAppender)collection[i]).Close(); } } this.RemoveAllAppender(); } catch (Exception ex) { AppUtil.LogEvent(ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } }
/// <summary> /// Stop the cache logging functionality. /// </summary> public static void Close(string cacheName) { lock (lockObj) { if (cacheName != null) { if (cacheName.Length != 0) { string temploggerName = LoggingInformation.GetLoggerName(cacheName); //called at remove cache if (temploggerName != null) { SetLevel(temploggerName, "OFF"); if (temploggerName != null) { log4net.ILog log = log4net.LogManager.GetLogger(temploggerName); log4net.Core.IAppenderAttachable closingAppenders = (log4net.Core.IAppenderAttachable)log.Logger; AppenderCollection collection = closingAppenders.Appenders; for (int i = 0; i < collection.Count; i++) { if (collection[i] is BufferingForwardingAppender) { //This FLUSH and close the current appenders along with all of its children appenders ((BufferingForwardingAppender)collection[i]).Close(); } } RemoveAllAppender(temploggerName); LoggingInformation.cacheLogger.Remove(cacheName); } } } } } }
static void ManageAppenderCollection(List <AppenderConfiguration> appenders, log4net.Core.IAppenderAttachable collection) { if (appenders != null) { foreach (var configuration in appenders) { if (configuration is RollingFileAppenderConfiguration) { CreateAppender(((RollingFileAppenderConfiguration)configuration).Merge(settings.DefaultAppenders.RollingFileAppender), collection); } else if (configuration is ConsoleAppenderConfiguration) { CreateAppender(((ConsoleAppenderConfiguration)configuration).Merge(settings.DefaultAppenders.RollingFileAppender), collection); } else { throw new Exception("Unable to create the appender. " + configuration.GetType().Name + "'s are not supported."); } } } }
static public void Write(object message, int priority, int eventid, System.Diagnostics.TraceEventType severity, string title, LogCategory logCategory) { log4net.ILog log = null; log4net.Core.IAppenderAttachable appenders = null; try { Monitor.Enter(padlock); // Intialize the default settings. if (settings == null) { try { // Initialize the log settings from the default appender configuration file. IntializeLogSettings(); // Custom appenders are optional to support overriding the default configuration. if (System.Configuration.ConfigurationManager.AppSettings["log4net_appenders_custom"] != null) { var custom = EnterpriseLibrary.Configuration.ConfigurationManager.GetConfiguration <LoggerConfiguration>("log4net_appenders_custom"); // Override the default settings with the defined custom values. settings = custom.Merge(settings); } // Create the default logger and access its appender collection. log = log4net.LogManager.GetLogger(settings.CategoryName); appenders = (log4net.Core.IAppenderAttachable)log.Logger; if (settings.AppenderCollection != null) { // Create the appenders and add them to the loggers collection. ManageAppenderCollection(settings.AppenderCollection.Appenders, appenders); } else { // No appenders were found. Create the appender(s) based on the defaults. if (settings.DefaultAppenders != null) { CreateAppender(settings.DefaultAppenders.RollingFileAppender, appenders); CreateAppender(settings.DefaultAppenders.ConsoleAppender, appenders); } else { throw new Exception("A default appender was not found. In order to support logging at least one default logger must be defined."); } } } catch (Exception ex) { // We were unable to initialize the settings without errors. settings = null; throw ex; } } // CUSTOM LOG CATEGORY // If a log category is defined use it. if (logCategory != null && !string.IsNullOrEmpty(logCategory.Name)) { // Check to see if the category's logger already exists. If it does then the category's appenders have already been created. if (log4net.LogManager.Exists(logCategory.Name) == null) { // Create the catgory's logger and it's appenders. // GetLogger() will create the logger and add it to the repository if it does not already exist so we // need to see if it exists before calling this method or we will not know that we need to create the appenders. log = log4net.LogManager.GetLogger(logCategory.Name); appenders = (log4net.Core.IAppenderAttachable)log.Logger; // Create the appenders. ManageAppenderCollection(logCategory.Appenders, appenders); } else { // Get the customized logger. log = log4net.LogManager.GetLogger(logCategory.Name); } } // DEFAULT APPENDER // A log category was not provided so the default must be used. else { // Get the default logger. log = log4net.LogManager.GetLogger(settings.CategoryName); } // Write the log message to the file. switch (severity) { case TraceEventType.Information: log.Info(message); break; case TraceEventType.Error: log.Error(message); break; case TraceEventType.Warning: log.Warn(message); break; } } catch (Exception ex) { throw ex; } finally { Monitor.Exit(padlock); } }