Thursday, 7 July 2016

Using Log4Net with Visual Basic .NET

Using Log4Net withVisual Basic .NET

Log4Net is the porting of Log4J into the .NET environment.
This is a simple introduction about using Log4Net with Visual Basic .NET.

Getting Log4Net dll and including it in a project

  • Download Log4Net from the Apache site: https://logging.apache.org/log4net/
  • then unzip the downloaded file into a folder, i.e. C:\Log4Net
  • In this way, in the folder C:\Log4Net\log4net-x.x.xx\bin\net  (where x.x.xx is the current version), you should find some subfolders, named as the .NET framework versions: 1.0, 1.1, 2.0, 3.5, 4.0, 4.5, ...
  • Inside each of them there is a release folder, and inside release ther's log4net.dll, which is the file you need.
  • In you project, add a reference to log4net.dll. 

Log4Net configuration file

Log4Net uses a configuration file named in this way:

<assemblyname>.log4net


For example:
VatCalculator.exe.log4net  
MathLibrary.dll.log4net


This is an example of Log4Net configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <appender name="MyLogAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="3" />
      <maximumFileSize value="500KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level - %message%newline" />
      </layout>
    </appender>
    <logger name="MyExampleLogger">
      <level value="ALL" />
      <appender-ref ref="
MyLogAppender" />
    </logger>
  </log4net>
</configuration>


You can see that there are 2 main sections:
  • appender
  • logger
The appender defines file name, type and behaviour.
The logger  specifies the name you use in your code to refer the logger.
In this example in the code we refer to the logger using the name MyExampleLogger.
This logger is linked to the appender MyLogAppender through the appender-ref tag.

In this way you can change the appender (so file name, behaviour, etc.) without changing the code.

The main features ot the appender section are:
  • the appender type: in this case we use the type log4net.Appender.RollingFileAppender, which intruduces a sort of rotation: when the log file reaches a certain condition (in this case the size), it's renamed (file name gets a suffix) and a new file is created. This process goes on till tha max number of files is reached, then the oldest file is deleted.
    In this way the log dimensions is kept under control.
  • the file tag sets the log file name
  • if the appendToFile tag is set to true, when the program starts and finds an existing log file, it appends new row to it; otherwise it creates a new file
  • The rollingStyle tag decides what is the conditions that forces the creation of a new file; in this case Size means that the condition is the size value you can find in the tag maximumFileSize.
  • maxSizeRollBackups is the max number of old log files kept, before the oldest one is deleted.
  • The layout tag contains information on how the log file rows are formatted

 Adding the configuration file to the project

  • Add a text file to the project, remember to use the right name assemblyname.exe.log4net
  • Paste the configuration from above into the file and make your customizations
  • In the file properties, set the property "Copy to output directory" to the value "Copy if newer"
  • Add this line to the AssemblyInfo.vb file:

    <Assembly: log4net.Config.XMLConfigurator(ConfigFile:="EasyStorLibCustom.dll.log4net", Watch:=True)>

    Note that specifying Watch:=True forces the program to reload the configuration when it's changed: in this way you can chage the configuration without restarting you application
  •  

Using the logger in the code

  • First of all a logger object must be created:

        Private _logger As log4net.ILog
      ...
      _logger = log4net.LogManager.GetLogger("MyExampleLogger")


    Note that the logger name is the one declarated in the logger name tag in the configuration file
  • Now the logger can be used to log informations. Log4Net provides some mothods to write the log file. The difference between them is the level of detail: you can write normal informations, debug, errors, fatal errors.
    The level can be logged in the file: see this config file extract, where the level is specified in the format:

       <conversionPattern value="%date [%thread] %-5level - %message%newline" />

    You can specify what levels you want to log, declaring it in the config file:
    For exapmple,

       <level value="ALL" />

    means that all level must be logged.

    These are the levels:

  •    ALL
       DEBUG
       INFO
       WARN
       ERROR
       FATAL
       OFF 

    The logger will be forced to log all the levels below the specified level.
      
  • The logger mothods reflect the levels:

          _logger.Debug(message)
       _logger.Info(message)
       _logger.Warn(message)
       _logger.Error(message)
       _logger.Fatal(message)

     

     


No comments:

Post a Comment