📜 ⬆️ ⬇️

NLog extension for error monitoring

Have you ever thought that using a log file is not very effective in correcting errors?

Want to fix bugs faster and easier?

Then read my article in which I will tell you how to monitor application errors using the NLog extension.
')


How it works?


The error monitoring solution should be suitable for existing applications, where a huge amount of code is written, which no one will redo for the sake of monitoring.

Error monitoring should be performed transparently, which means that there should be no code that monitors the error handlers.

Let's take a closer look at the typical exception handling code:



What can be used as an entry point for error monitoring?

True, the logging system will help us.

The logging system must have an extension that will collect, group and send errors to the monitoring system. The monitoring system will send a notification to the developers, the developers will correct the error.

Below is a diagram of how this works:



This is the scheme of work that the NLog extension for the Zidium monitoring system implements. What is Zidium? This is a cloud application monitoring service that can be used for free.

You can install the extension through the nuget-package - NLog.Zidium

Learn more about the expansion in the documentation .

Hello world


To show how everything works, we will create a simple console application and connect it to the monitoring system. Let the application be an error, which is recorded in the NLog.

Application code

using NLog;
using System;

namespace NLog2
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        
        static void Main()
        {
            logger.Info("  ");
            try
            {
                throw new Exception("");
            }
            catch (Exception exeption)
            {
                logger.Error(exeption, "  ");
            }
            LogManager.Flush();
        }
    }
}

NLog

PM > Install-Package NLog.Zidium

Zidium.xml

Zidium.xml , exe- .

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <access accountName="Test" secretKey="..." />
  <defaultComponent id="3826486a-66ce-4a50-a310-4c87deaf01db" />
</root>

:

accountName – ,
secretKey – API,
defaultComponent id – ID , .

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogLevel="Trace" 
      internalLogFile="nlog-internal.log">
  <targets>
    <target name="ZidiumErrors" xsi:type="Zidium.Errors" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="ZidiumErrors" />
  </rules>
</nlog>



, .

, Hello World , email-. .


. .

.

Source: https://habr.com/ru/post/347528/


All Articles