Logging Using Log4net in Compact Framework 2.0 in Visual Studio 2008

Contents

What is log4net?
Advantages of log4net.
To do logging using log4net.dll in CF 2.0 we need to do a couple of things.
Use a new Device Application project from Smart device project in Visual Studio 2008.
Convert Log4net.dll into a .Net Compact Framework 2.0 Class Library (*.dll).
Add the new log4netCF.dll to the project using "Add reference".
Now you will see the output in the simulator.

Figure 1 Select Smart Device Project
Figure 2 Select Device Application
Figure 3 Device Application Main Form
Figure 4 Errors using log4Net
Figure 5 Change Project Properties
Figure 6 Change Project Properties
Figure 7 Add Log4netCF.dll
Figure 8 Config.xml File
Figure 9 Change Properties of Config.xml File
Figure 10 Add 3 Buttons to Main Screen
Figure 11 Create Logger
Figure 12 Create Log Button Click Event Code
Figure 13 Open Log Click Event Code
Figure 14 Clear Button Click Event Code
Figure 15 Opt 1 Main Screen
Figure 16 Opt 2 Click On Create Log Button
Figure 18 Opt 4 Click Open Log File to read It
Figure 17 Opt Log.txt is Created Now

What is log4net?

Log4net is an open source library that allows .NET applications to log output to a variety of sources (for example, the console, SMTP or files). Log4net is a port of the popular log4J library used in Java. The full details of log4net can be found at http://logging.apache.org/log4net/index.html

Advantages

The advantages of log4net are:

1. Works with .NET 1.0 & 1.1: The much improved logging of EntLib 2.0 and above is only available if your application is running on .NET 2.0 or greater. log4net however works on all versions of .NET.

2. Simpler install: When using the Enterprise Library there are some services you really should install. This is as simple as running a bat file included with EntLib but it does complicate your deployment process.

3. Slightly faster: log4net was significantly quicker than EntLib 1.1 logging. EntLib 2.0 onwards has improved its performance but log4net remains slightly faster. A benchmark I found while researching my presentation had EntLib taking approximately 5-6 seconds to log 100,000 entries while log4net took about 3 seconds. Does the speed difference matter? Probably not. However log4net net does support.

4. Appender Buffering: Buffering support with some appenders lets log4net queue up log entries and write them in a single batch. If you are writing entries to the database then buffering is a good way to improve performance.

5. More platforms: The Enterprise Library does not support the .NET Compact Framework whereas log4net does.

To do logging using log4net.dll in CF 2.0 we need to do a couple of things; they are:

  • We must use the Compact Framework 2.0 installed in our computer or you can download it from: http://www.microsoft.com/en-in/download/details.aspx?id=22808
  • We need to download the log4net.dll from the link: http://logging.apache.org/log4net/download.html. We need the Source Code of that log4net.dll.
  • We need to convert the log4net.dll into a DLL that can work in the Compact Framework 2.0.

Using the project

Now to use the new Device Application project from the Smart device project in Visual Studio 2008.

To use new Device application project,

go to the "File" menu in Visual Studio 2008  then select "New" => "Project..." then select "Smart Device Project".

fig1.jpg

Figure 1 Select Smart Device Project

Press "Ok" and now select "Device application Project".

fig2.jpg

Figure 2 Select Device Application

Press "Ok" and you will see the screen like this:

fig3.jpg

Figure 3 Device Application Main Form

Conversion

Now to convert the Log4net.dll into the .Net Compact Framework 2.0 Class Library (DLL file).

If you use the log4net.dll then it will give you an error like this after writing the code for the logging data from the application.

fig4.jpg

Figure 4 Errors using log4Net

Why do we need to covert?

The problem with log4net and its Compact Framework support is that is mostly not maintained over the time. Or Simply We are making the .dll file that can compatible with Compact Framework 2.0.

So we need to convert the log4net.dll to a CF 2.0 Class Library (DLL file) and then you can use that in you project.

So here is the procedure:

  • Open the log4.net src/log4net.vs2008.sln from the downloaded source of log4net.
  • Add a new Smart Device class library project (WM5 SDK and CF 2.0 is OK) and name it log4netCF
  • delete the existing class1.cs file.
  • Change the project properties
fig5.jpg

    Figure 5 Change Project Properties

    Set the Assembly name and the default namespace = "log4net", then:

    • Right-click every source code folder in the log4net.vs2008 project in the Solution Explorer and select copy and then right-click the log4netCF project and select paste; repeat that for every single folder
    • Appender, Config, Core, DateFormatter, Filter, Layout, ObjectRenderer, Plugin, Repository and Util
    • Also copy the single files:
    • AssemblyInfo.cs ,AssemblyVersionInfo.cs, GlobalContext.cs, ILog.cs, LogicalThreadContext.cs, LogManager.cs, MDC.cs, NDC.cs and ThreadContext.cs from log4net.vs2008 into the log4netCF project.
    • Delete Properties/AssemblyInfo.cs in the log4netCF project
    • add PocketPC;NETCF_2_0;NETCF to the build options of log4netCF
    fig6.jpg

      Figure 6 Change Project Properties

      Now right-click the log4netCF project and select Build. It should build without any error and you have a working Compact Framework 2.0 log4net assembly.

      Add the DLL to the project

      Add the new log4netCF.dll to the project using "Add reference".

      We need to provide a reference of the newly created log4netCF.dll to our project.

      fig7.jpg

      Figure 7 Add Log4netCF.dll

      Add the new Config.xml file to your project or you can create a file with the name config.xml and add it to that file to the source project as a existing item.

      The Config.xml file contains a LogFileAppender and a DebugAppender and they are both set to log all levels.

      fig8.jpg

      Figure 8 Config.xml File

      Change the Properties of config.xml file "Copy to output directory =Copy Always".

      fig9.jpg

      Figure 9 Change Properties of Config.xml File

      I have used 3 buttons and 1 TextBox with multiline enabled on the Main Form; see the following image.

      Create log Button will create the log file and if it is exists then it will append the log to the file.

      Open Log File Button will read the log file and display on the screen.

      Clear button will clear the screen.

      fig10.jpg

      Figure 10 Add 3 Buttons to Main Screen

      Create the logger; see the following image:

      fig11.jpg

      Figure 11 Create Logger

      On the click event of the Create Log Button, write the code as in
      the following:

      fig12.jpg

      Figure 12 Create Log Button Click Event Code

      On the click of the Open Log button:

      fig13.jpg

      Figure 13 Open Log Click Event Code

      On click of the Clear Button:

      fig14.jpg

      Figure 14 Clear Button Click Event Code

      Run the project and select the deploy option.

      fig15.jpg

      It will deploy your project to the Simulator.

      Now you will see the output in the Simulator.

      fig16.jpgfig17.jpg
      fig18.jpgfig19.jpg

      Conclusion: We can use log4net.dll in the Compact Framework 2.0 and its easy and stable. Thanks for reading.

        

      Up Next
        Ebook Download
        View all
        Learn
        View all