Navigation

Showing posts with label nLog. Show all posts
Showing posts with label nLog. Show all posts

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.

Wednesday, May 12, 2010

Jarek Kowalski – Nlog Founder

I have been using Nlog for a number of years and find it to be a great logging component.  Today my post on loadFromRemoteSources which deals with referencing assemblies from remote network shares in VS2010 was re-tweeted by Jarek on his twitter page!

http://twitter.com/JarekKowalski

image

Wednesday, May 05, 2010

How to trap Nlog events within your application

Being able to trap nLog log events within your application allows you to provide a default msg to your users without having to have the UI code everywhere in your application. 

This post describes how to setup nLog with an additional target that will fire an event in your application that you can then capture.

First there are a few changes to the log file;

image

Notice that we added a new target and rule for that target.  Next we need to add a class with a shared method to capture the event;

image

This allows you to catch the log event.

Sunday, April 18, 2010

Application Logging with nLog

Logging is a vital component of any successful application. Effective logging within any application is a mix of art and science. Over the years I have used a number of logging solutions with mixed results; however, over the last few years I have been using nLog with great success.

Below are some links to nLog resources.

http://nlog-project.org/
http://nlog.codeplex.com/
http://www.codeproject.com/KB/trace/nlog.aspx