Fun cu NLOG si async

Sa presupunem ca avem o aplicatie simpla console in care facem logging. Pentru mine cel mai usor este NLOG.

Codul este:

using System;


namespace ConsoleApplicationNlog
{
    class Program
    {
        static void A()
        {
            try
            {
                B();
            }
            catch (Exception ex)
            {
                
                throw new Exception("from a", ex);
            }
        }
        static void B()
        {
            throw new ArgumentException("from b");
        }
        static void Main(string[] args)
        {
            var s = NLog.LogManager.GetCurrentClassLogger();
          
            try
            {
                A();
            }
            catch(Exception ex)
            {
                s.ErrorException(ex.Message,ex);
                Console.WriteLine("---------------------");
                //Console.WriteLine(ex.StackTrace);
            }
            //Thread.Sleep(1000);
        }
    }
}

 

Vrem sa logam in fisier stack trace si eroarea si pentru asta construim fisierul de configurare Nlog.config urmator

<?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”>

  <targets async=”true“>
    <target xsi:type=”File” name=”f” fileName=”${basedir}/logs/${shortdate}.log”
            layout=”${longdate} ${uppercase:${level}} ${message}  ${exception:format=ToString:innerFormat=StackTrace:maxInnerExceptionLevel=2} (${callsite:includSourcePath=true})”  />
    
  

    </targets>

  <rules>
    <logger name=”*” minlevel=”Trace” writeTo=”f” />   
  </rules>
</nlog>

 

Cind ne uitam in ceea ce logheaza este:

2016-01-30 08:48:24.3290 ERROR from a  System.Exception: from a —> System.ArgumentException: from b
   at ConsoleApplicationNlog.Program.B()
   at ConsoleApplicationNlog.Program.A()
   — End of inner exception stack trace —
   at ConsoleApplicationNlog.Program.A()
   at ConsoleApplicationNlog.Program.Main(String[] args)
   at ConsoleApplicationNlog.Program.B()
   at ConsoleApplicationNlog.Program.A() (ConsoleApplicationNlog.Program.Main)

 

 

Unde sunt liniile din stack trace?

 

Rezolvare:

Fie puneti

<async=”false“>

fie decomentati

Thread.Sleep(1000);

In cazul acesta , o sa arate asa:

2016-01-30 08:56:30.6137 ERROR from a  System.Exception: from a —> System.ArgumentException: from b
   at ConsoleApplicationNlog.Program.B() in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 22
   at ConsoleApplicationNlog.Program.A() in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 12
   — End of inner exception stack trace —
   at ConsoleApplicationNlog.Program.A() in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 17
   at ConsoleApplicationNlog.Program.Main(String[] args) in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 30
   at ConsoleApplicationNlog.Program.B() in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 22
   at ConsoleApplicationNlog.Program.A() in C:\p\ConsoleApplicationNlog\ConsoleApplicationNlog\Program.cs:line 12 (ConsoleApplicationNlog.Program.Main)

Leave a Reply

Your email address will not be published. Required fields are marked *