Logging is useful to track application behavior and troubleshooting. There are many ways to accomplish this task, and one popular way is with log4net. It is a free library that developer can use to log application behavior to text files.

In this article, I will show steps by steps using log4net in the Mi-Forms environment.

Step 1:

Donwload log4net dll at:


Step 2:

Add log4net dll as a reference in the Mi-Forms Designer .NET Script editor

Step 3:

Setup default configuration for log4net:

Sub Log4NetConfig(ByVal logFileName As String)
	Dim layout As log4net.Layout.PatternLayout = New log4net.Layout.PatternLayout()
    	layout.ConversionPattern = "%date{MM-dd-yyyy HH:mm:ss:ffff} [%t] %-5p %logger - %m%n"
	Dim appender As log4net.Appender.RollingFileAppender = New log4net.Appender.RollingFileAppender()
   	appender.Layout = layout
    	appender.File = logFileName
	appender.AppendToFile = True
    	appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size
    	appender.MaxFileSize = 1024
    	appender.MaxSizeRollBackups = 5
    	appender.ImmediateFlush = True
	appender.Threshold = log4net.Core.Level.Off
	Dim stdout As log4net.Appender.ConsoleAppender = New log4net.Appender.ConsoleAppender()
    	stdout.Layout = layout
End Sub


Step 4:

Declare a global variable and use for the whole form logic.

Dim _log As log4net.ILog

Step 5:

Call the above method Form_AfterOpen event as below:



<MiCode(FormScriptEventType.AfterOpen)> _ 
    Public Sub Form_AfterOpen(ByVal e As AfterOpenEventArgs)
	_log = log4net.LogManager.GetLogger("Instance 1")
	_log.Debug("test 1")
	Dim _log1 As log4net.ILog = log4net.LogManager.GetLogger("Instance 2")
	_log1.Debug("test 2")				
    End Sub



Step 6:

Test log file:

09-11-2014 15:14:26:3816 [1] DEBUG Instance 1 - test 1
09-11-2014 15:14:26:3986 [1] DEBUG Instance 2 - test 2

You can use the log file to troubleshoot the form behavior and save a lot of time.