When reading the log4net manual I figured out even more simpler way to put logging in VB.Net applications.
All you have to do:
- Add reference to log4net.dll
- Add imports for log4net and log4net.Config namespaces (either in project level or in each Class file in your app)
- Call once of the log4net’s BasicConfigurator class ‘Configure’ method in your main Class.
- Create logger instance in each of your Classes
With this solution you’ll get quicky started on logging, and it has following advantages:
- You don’t need to create and configure .config file at all
- You don’t have to open the log file to see what is happening there as you can use Vs.NET “Output window” instead (but you don’t have to spend time configuring it either !)
- You’ll get the class name automatically printed into messages.
See also there posts on the same subject:
Follow the example below:
First create new Windows applicarion projct, it comes with a form called “Form1″.
Add reference to log4net.dll (localized in the log4net installation directory.
Then add two buttons:
- Button1
- Button2
Here is an example of the layout:

Add following code to Form1 (select first all the existing code in the Form1 and copy this over it)
Imports log4net
Imports log4net.Config
Public Class Form1
Private Shared log As log4net.ILog
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
BasicConfigurator.Configure()
log.Debug("Form1_Load")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
log.Debug("Calling Class1")
Dim objClass1 As New Class1
objClass1.DoIt()
log.Debug("Back in Form1")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
log.Debug("Calling Class2")
Dim objClass2 As New Class2
objClass2.DoIt()
log.Debug("Back in Form1")
End Sub
End Class
Then create two new classes, Right-click on project name, choose “Add/Class”, in the dialog window just type “Class1.vb” in the “Name” field and hit “Add”.
Repeat this in order to create Class2.vb
Add the following code for Class1.vb (select first all the existing code in the Class1 and copy this over it)
Imports log4net
Public Class Class1
Private Shared log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Public Sub DoIt()
log.Debug("Method called")
End Sub
End Class
Add following code for Class2.vb (select first all the existing code in the Class2 and copy this over it)
Imports log4net
Public Class Class2
Private Shared log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Public Sub DoIt()
log.Debug("Method called")
End Sub
End Class
Then just Run your application, choose “Debug/Start Debugging” or just hit “F5″.
When your app is running, open the VS.NET “Output” window (“Ctrl+Alt+O”), if it’s not already open.
Click the buttons on your app, you should see some messages on the output window now..
Note that when using reflection in the logger declaration you will log your class name automatically in every message.
Just put the same declation, like in the example below, in every class that your project has and keep logging !
Private Shared log as ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType










October 20, 2007 at 1:02 pm
Is there a way to avoid adding this “LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType” in every class?
October 22, 2007 at 3:09 pm
Patrick,
You can declare a global variable for logging somewhere in your code:
Dim log As log4net.ILog
log = log4net.LogManager.GetLogger(“MyApp”)
Just remember to configure properly your project’s .config file (for “MyApp”). See my other post for more details:
http://vpalmu.wordpress.com/2007/09/04/simple-log4net-example-with-vbnet/
February 27, 2009 at 8:26 pm
[...] Posts Simple Log4Net example with VB.NetSimplest ever example for Log4Net and VB.NETTip: Visual basic 6 error [...]
April 27, 2009 at 5:54 am
[...] VPalmu: Simplest ever example for Log4Net and VB.NET [...]
May 5, 2009 at 8:23 am
Just to add a little touch of complexity, but no more than needed
If you wan’t do declare a file logger, use:
Dim layout As ILayout = New SimpleLayout()
Dim fileAppender As New FileAppender(layout, “c:\Test.log”)
BasicConfigurator.Configure(fileAppender)
March 5, 2010 at 11:02 am
This helped me implement Log4Net. Thanks for the tutroial.
January 11, 2012 at 5:42 pm
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType should only be used in an abstract base class. For all other cases use the current class directly.