Navigation

Wednesday, June 02, 2010

Windows Mobile Client Development (OS 5) Logging with Nlog

I just completed a windows mobile client application targeting a 5.1 mobile OS.  This project provided the ability to scan a barcode with the embedded barcode scanner, return the selected item from a web service (along with a photo stored on a network share) and allow the user to post movement and status changed on the scanned item.

This was the first time I used NLog on a Compact Framework device and after getting use to some of the minor issues related to the OS was able to use NLog just like a VB.net application.

In this project I configured Nlog via code rather then a configuration file.  The first issue was getting the application path to pass to Nlog.

First, I created a global variable to save the path and the Nlog logger;

Public gApplicationPath As String = String.Empty

Public gLogger As NLog.Logger = Nothing

 

Next I set the path with the following statement;

gApplicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

The configuration was simple with the following code;

NLog.Internal.InternalLogger.LogFile = gApplicationPath & "\nlog.txt"
NLog.LogManager.ThrowExceptions = False

Dim config As New NLog.Config.LoggingConfiguration
Dim filetarget As New NLog.Targets.FileTarget
With filetarget
    .Name = "filetarget"
    .FileName = gApplicationPath & "\ErrorLog.txt"
    .Layout = "${longdate}||${level}||${logger}|||${message}|${exception:format=message:separator=*}^"
End With
config.AddTarget("file", filetarget)
Dim rule1 As New NLog.Config.LoggingRule("*", NLog.LogLevel.Trace, filetarget)
config.LoggingRules.Add(rule1)
NLog.LogManager.Configuration = config
gLogger = LogManager.GetLogger("myLogerName")

Pretty straightforward!

I am a big fan of targeting a database to store the logging data to and will address updating the application to use that target next time.

1 comment:

eliza harvey said...

Hello, M Eliza working with Windows Mobile Development
and would like to say that thanks for this post.

Regards...
Eliza Harvey.