Modifying log4net Configuration

CompleteFTP's logging system is based on log4net, a highly configurable logging framework. This guide demonstrates how to modify and extend the logging configuration to meet your specific needs.

1. Create a Custom Configuration File

To prevent your changes from being overwritten during upgrades:

2. Copy the Default Configuration

Locate the default log4net configuration in:

Copy the <log4net> node (and its sub-nodes) into your new LogConfig.xml file. This copied configuration serves as your base for further customizations.

3. Modify the Configuration

The log4net configuration consists of the following key components:

  1. Appenders: Define where log messages are written, such as files, the Windows Event Log, or a remote server. Each appender has its own settings, such as file paths, message layouts, and filtering rules.
  2. Filters: Include or exclude specific log messages based on criteria like severity levels, substrings, or loggers.
  3. Layouts: Define the format of log messages, including timestamp, severity, and message content.
  4. Root Logger: The <root> node ties everything together, specifying the global logging level (e.g., ALL, INFO, ERROR) and listing the appenders to use.

Steps to Modify the Configuration

  1. Edit Existing Appenders: Locate the appender you want to modify in the <log4net> section of your configuration file. Update its settings, such as file paths, rolling file limits, or filters.
  2. Add New Appenders: Define new appenders in the <log4net> section for additional functionality (e.g., sending logs to a syslog server). Be sure to include all required parameters for the new appender.
  3. Reference Appenders in the Root Logger: After modifying or adding appenders, ensure they are listed in the <root> section. For example:
    
    <root>
      <!-- Default appenders -->
      <appender-ref ref="Audit" />
      <appender-ref ref="Config" />
      <appender-ref ref="Recent Logging Messages" /> <!-- Modified -->
      <appender-ref ref="Recent Error Messages" />
      
      <!-- New appenders -->
      <appender-ref ref="FilteredRollingFile" /> <!-- New appender for filtered logs -->
      <appender-ref ref="EventLog" />           <!-- New appender for Windows Event Log -->
      <appender-ref ref="Syslog" />             <!-- New appender for remote syslog -->
      
      <level value="ALL" />
    </root>
    				
  4. Restart the CompleteFTP Service: Once your changes are saved in the LogConfig.xml file, restart the CompleteFTP service to apply the updated configuration.

Examples of log4net Customizations

Below are common examples of customizations you can apply to the logging configuration.

Example A: Customizing the 'Recent Logging Messages' Appender


<appender name="Recent Logging Messages" type="log4net.Appender.RollingFileAppender">
  <encoding value="utf-8" />
  
  <!-- Filters (unchanged) -->
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="Audit" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="ConfigLog" />
    <acceptOnMatch value="false" />
  </filter>
  
  <!-- File settings -->
  <file type="log4net.Util.PatternString">
    <converter>
      <name value="folder" />
      <type value="EnterpriseDT.Util.Debug.SpecialFolderPatternConverter,CompleteFTPServer" />
    </converter>      
    <conversionPattern value="%folder{CommonApplicationData}\Enterprise Distributed Technologies\Complete FTP\logs\Diagnostics.log" />
  </file>
  
  <param name="AppendToFile" value="true" />
  
  <!-- Customize the size and backup settings -->
  <param name="MaxSizeRollBackups" value="30" />
  <param name="MaximumFileSize" value="10MB" />
  <param name="RollingStyle" value="Size" />
  <param name="StaticLogFileName" value="true" />
  
  <!-- Layout -->
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%date %level %logger %message %newline" />
  </layout>
</appender>
		

Example B: Filtering Log Messages Based on Substrings


<appender name="FilteredRollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="logs/filtered.log" />
  <appendToFile value="true" />
  
  <rollingStyle value="Size" />
  <maximumFileSize value="5MB" />
  <maxSizeRollBackups value="5" />
  
  <!-- Filters to exclude specific substrings -->
  <filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="SFTP" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="admin" />
    <acceptOnMatch value="false" />
  </filter>
  
  <!-- Deny any message not explicitly allowed -->
  <filter type="log4net.Filter.DenyAllFilter" />
  
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d [%t] %c - %m%n" />
  </layout>
</appender>
		

Example C: Logging to the Windows Event Log


<appender name="EventLog" type="log4net.Appender.EventLogAppender">
  <logName value="Application" />
  <applicationName value="CompleteFTP" />
  
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d [%t] %c - %m%n" />
  </layout>
</appender>
        
;

Example D: Logging to a Remote Syslog Server


<appender name="Syslog" type="log4net.Appender.RemoteSyslogAppender">
  <param name="RemoteAddress" value="192.168.1.100" />
  <param name="RemotePort" value="514" />
  
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d [%t] %c - %m%n" />
  </layout>
</appender>
		

By following these steps and referring to the examples, you can customize CompleteFTP's logging system to meet the specific needs of your environment.