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>
    <!-- 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>