Logging is very important for development and for production.
Logger
The logger i have found is NLog .
It can be downloaded here.
Adding to project
Next you need to add configuration file by Add->New Item and choosing "Empty NLog Configuration File" :
This will add a configuration file name NLog.config and a reference to NLog.dll.
Next change NLog.config properties "Copy to Output Directory" to "Copy always"
Logging API
Most important classes are Logger and LogManager.
Logger class represent the name of the logger and has methods for writing to the log.
LogManager create and manage the Logger instances.
You create log in the following way :
Logger logger = LogManager.GetLogger("someLogName");
The following log level are supported :
- Trace - relevant in general for development
- Debug - not enabled for production
- Info - information
- Warn - Warning
- Error - Errors
- Fatal - Fatal Error
Writing to Log file :
logger.Log(LogLevel.Error, "Some Error - {0}",i);
LogLevel has all log level described above.
Supported Targets
- Console
- Memory - store trace in memory
- MethodCall - call a user defined method
- Network,NLogViewer,Chainsaw - Network using HTTP , HTTPS
- WebService - using SOAP,POST
- LogReciverService - use a WebService using WCF
Configuration file :
The are two important sections :
targets
- Can have one of more target tag
- possible target attributes :
- name
- xsi:type
rules
- Can have one of more logger tags
- possible rule attributes :
- name
- minlevel
- writeTo
for example
Remark :
- Set EnableConsole=1 under [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\XDE] in case you want to use Console target (32 bit PC)
Sample source
NLog_NK.zip
This sample illustrate a usage of the logger with console target.
Add NLog.config as described before.
Click the button to produce log events
MainPage.xaml.cs
NLog.config
Run the application and click the button to produce
Sample source
NLog_OutputWindow_NK.zip
This sample illustrate a usage of the logger with MethodCall target to write to the Output window.
Add NLog.config as described before.
Click the button to produce log events
MainPage.xaml.cs
NLog.config
MyClass.cs
Run the application via the debugger, open the Output windows and click the button to produce :
Nathan