Sunday, 30 July 2017

logging using nLog

nlog is free logging platform which use in .net, mobile platform. Its very easy to use and integrated with Visual studio Nuget package manager.

1.       Creating console application to check how to use nLog.


2.       Now add the nlog from Nuget package.

Add both nlog and nlog Configuration file.


3.       Now you can see that it added the Nlog dll in reference and Nlog config file.


4.       Let’s see the configurations.
5.       You need only below namespace for configuration. Others you can remove.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

6.       Now you can select the target to where you want to maintain your logs by adding or updating the below tags
  <target xsi:type="EventLog" name="eventLog" layout="${longdate} ${uppercase:${level}} ${message}" source="MyMachine" />

Source will use to identify in Event log.
There are many log type listed.


Lets select the event log.

7.       You can set the rules for which condition log will happens.
<logger name="*" minlevel="Debug" writeTo="eventLog" />
MinLevel of log is : debug
WriteTo for target as eventLog
Now it will log when ever we have error, Info, warn, debug.

8.       Let’s move to the coding side.
a.       Import Nlog into your file as like below:
using NLog;
b.      Create an Variable for logger  class.
Logger _logger = LogManager.GetCurrentClassLogger();
c.       Lets create two method one for logging the debug information and another for Error log.

  public static void DubugLog(string message)
        {
            _logger.Debug(message);
        }

public static void ErrorLog(string message)
        {
            _logger.Error(message);
        }

You can see the full code of log.
namespace logtest
{
    public class LogHelper
    {
       private static Logger _logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// logging the debug
        /// </summary>
        /// <param name="message"></param>
        public static void DubugLog(string message)
        {
            _logger.Debug(message);
        }


        /// <summary>
        /// logging the Error
        /// </summary>
        /// <param name="message"></param>
        public static void ErrorLog(string message)
        {
            _logger.Error(message);
        }
    }
}

d.      Call the method from program class.
Doing multiplication passing, two values

static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter value for multiply");
                int first = Convert.ToInt32( Console.ReadLine());
                int second = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Output {0}", Multiply(first, second));
            }
            catch(Exception ex)
            {
                LogHelper.ErrorLog(ex.Message);
            }

        }

        public static int Multiply(int i, int j)
        {
            LogHelper.DubugLog("You are in Program class and Multiply function");
            int  m = i / j;

            LogHelper.DubugLog("You are exit Program class and Multiply function");
            return m;
        }

9.       Now run the application.
10.   See in the Event viewer, open the event viewer.
Check into application log.
You can see below that two records are logged for source MyMachine.

One is information another is error.
Click on information you will see message like below which ever you passed.


Click on error you will see the error you got from your application.



No comments:

Post a Comment

Others

You will like to read it