ttajic
6/29/2016 - 10:32 AM

nlog sample

nlog sample

using NLog;

public class SomeClass {
  private Logger Log = LogManager.GetCurrentClassLogger();
  
  ...
  
  
  Log.Info("orderNo: {0}, approved {1}, user: {2}, preparedBy: {3}", order.No, approved, usr, request.Order.PreparedByUser);
  Log.Error("");
  Log.Warning("");

  
  
  
... PUT this config file (NLog.config) in bin folder

<?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"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >

  <!-- 
        See http://nlog-project.org/wiki/Configuration_file 
        for information on customizing logging rules and outputs.
         -->
  <targets>
    <!-- encoding="Unicode" is not the default so use it -->
    <target xsi:type="File" name="Accounts" fileName="${basedir}/logs/Accounts/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" encoding="Unicode" />
    <target xsi:type="File" name="Contacts" fileName="${basedir}/logs/Contacts/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" encoding="Unicode" />
    <target xsi:type="File" name="SalesOrders" fileName="${basedir}/logs/SalesOrders/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" encoding="Unicode" />

    <!-- puts a file under logs subdirectory, which is in bin folder for console apps, and under root directory of web app -->
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
    <target xsi:type="ColoredConsole" name="c" layout="{uppercase:${message}}" useDefaultRowHighlightingRules="true" />
  </targets>

  <rules>
    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    -->
    <logger name="*" minlevel="Trace" writeTo="f,c" />
  </rules>
</nlog>