Matheo
Matheo

Reputation: 35

How to execute nLog?

I am having a data written using nLog

Code snippet:

        <target xsi:type="File"
            name="AAAAA"
            filename="${basedir}\..\AAAAA\AAAAA${shortdate}.csv">
        <layout xsi:type="CsvLayout" delimeter="Semicolon" withHeader="True" quoting="Auto">
            <column name="Date" layout="${longdate}" />
            <column name="Thread" layout="${threadid} (${threadname})" />
            <column name="Level" layout="${level}" />
            <column name="Logger" layout="${logger:shortName=False}" />
            <column name="NDC" layout="${ndlc:separator=, }" />
            <column name="Message" layout="${message}" />
        </layout>
    </target>

How do I execute that logger? And where is the best place to do that?

Upvotes: 0

Views: 146

Answers (1)

Rafal
Rafal

Reputation: 12619

To make NLog log into your file, on top of your target, you just need a logging rule in the config that refers that target:

...</target></targets>
<rules>
  <logger name="*" minlevel="Trace" writeTo="File" />
</rules>

after you add that your code should just trigger this file to be created just by logging - so nothing special required in code

   ILogger _logger = LogManager.GetCurrentClassLogger();
   _logger.Info("Work started");

If that does not work it might be that your configuration is invalid and by default NLog will not throw exceptions and silently fail to log. You can change that behaviour by adding attributes to nlog configuration node:

<nlog throwConfigExceptions="true" internalLogFile="c:\temp\nlog-internal.txt" 

with that you will be able to see why your logger is not working.

Upvotes: 1

Related Questions