Logging Framework with Tungsten TotalAgility
Logging is an essential part of any application and it becomes a critical part of the custom applications built using Tungsten TotalAgility. For implementing custom solution in Tungsten TotalAgility, .Net Assemblies are utilised and for the logging feature, we do not want to re-invent the wheel by writing from-the-scratch logging components.
Yes, the solution is taking advantage of open-source logging frameworks available in the market and there are plenty !!!
However, choosing a open-source framework that fits in our application is the key factor as these components becomes an integral part and there is no turning back. After a spending sometime on look out suitable for logging frameworks, log4net and NLog were the candidates for us to evaluate.
Both are solid logging frameworks and capabilities are immense. However, there are subtle differences which over weighs one with another. And finally we chose NLog as the logging framework for our product and custom implementations.
Why did we choose NLog over log4net
- NLog is setup is seamless.
- log4net code base has not been updated for last three years. A question may rise why a framework needs an update if it is stable. The answer is very subjective, however as open-source framework we see more the updates/changes, the code base becomes more stable and it evolves with time.
- NLog code base is more cleaner than log4net.
Okay… Now we will setup NLog in a .Net project and it is literally a cake-walk.
Install nuget package “NLog -Version 4.7.0” using following command in Package Manager Console for the desired Visual Studio project.
To enable logging in a custom database, copy the following in the <configuration> section of web.config/app.config and configure the database connection details
Above configuration will enable NLog to write the log to a text file to ‘c:\temp\myapplication.log’ and as well as in ‘acmo_log’ database table.
Use the following code in the class globally to initialize the Logger object
NLog supports the following log levels.
- Trace – very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during development
- Debug – debugging information, less detailed than trace, typically not enabled in production environment.
- Info – information messages, which are normally enabled in production environment
- Warn – warning messages, typically for non-critical issues, which can be recovered or which are temporary failures
- Error – error messages – most of the time these are Exceptions
- Fatal – very serious errors!
Use the “Logger” object to utilise the different log level methods as mentioned below.