Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Logging different levels depending on whether or not there is an error</h2> <p>This example allows you to get more information when there is an error in your code. Basically, it buffers messages and only outputs those at a certain log level (e.g. Warn) <strong>unless</strong> a certain condition is met (e.g. there has been an error, so the log level is >= Error), then it will output more info (e.g. all messages from log levels >= Trace). Because the messages are buffered, this lets you gather trace information about what happened <strong>before</strong> an Error or ErrorException was logged - very useful!</p> <p>I adapted this one from <a href="http://github.com/jkowalski/NLog/blob/master/examples/NLogDemo/NLog.config" rel="noreferrer">an example in the source code</a>. I was thrown at first because I left out the <code>AspNetBufferingWrapper</code> (since mine isn't an ASP app) - it turns out that the <a href="http://nlog-project.org/wiki/PostFilteringWrapper_target" rel="noreferrer">PostFilteringWrapper</a> requires some buffered target. Note that the <code>target-ref</code> element used in the above-linked example cannot be used in NLog 1.0 (I am using 1.0 Refresh for a .NET 4.0 app); it is necessary to put your target inside the wrapper block. Also note that the logic syntax (i.e. greater-than or less-than symbols, &lt; and >) has to use the symbols, not the XML escapes for those symbols (i.e. <code>&amp;gt;</code> and <code>&amp;lt;</code>) or else NLog will error.</p> <p>app.config:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/&gt; &lt;/configSections&gt; &lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true" internalLogToConsole="true" internalLogLevel="Warn" internalLogFile="nlog.log"&gt; &lt;variable name="appTitle" value="My app"/&gt; &lt;variable name="csvPath" value="${specialfolder:folder=Desktop:file=${appTitle} log.csv}"/&gt; &lt;targets async="true"&gt; &lt;!--The following will keep the default number of log messages in a buffer and write out certain levels if there is an error and other levels if there is not. Messages that appeared before the error (in code) will be included, since they are buffered.--&gt; &lt;wrapper-target xsi:type="BufferingWrapper" name="smartLog"&gt; &lt;wrapper-target xsi:type="PostFilteringWrapper"&gt; &lt;!--&lt;target-ref name="fileAsCsv"/&gt;--&gt; &lt;target xsi:type="File" fileName="${csvPath}" archiveAboveSize="4194304" concurrentWrites="false" maxArchiveFiles="1" archiveNumbering="Sequence" &gt; &lt;layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false"&gt; &lt;column name="time" layout="${longdate}" /&gt; &lt;column name="level" layout="${level:upperCase=true}"/&gt; &lt;column name="message" layout="${message}" /&gt; &lt;column name="callsite" layout="${callsite:includeSourcePath=true}" /&gt; &lt;column name="stacktrace" layout="${stacktrace:topFrames=10}" /&gt; &lt;column name="exception" layout="${exception:format=ToString}"/&gt; &lt;!--&lt;column name="logger" layout="${logger}"/&gt;--&gt; &lt;/layout&gt; &lt;/target&gt; &lt;!--during normal execution only log certain messages--&gt; &lt;defaultFilter&gt;level &gt;= LogLevel.Warn&lt;/defaultFilter&gt; &lt;!--if there is at least one error, log everything from trace level--&gt; &lt;when exists="level &gt;= LogLevel.Error" filter="level &gt;= LogLevel.Trace" /&gt; &lt;/wrapper-target&gt; &lt;/wrapper-target&gt; &lt;/targets&gt; &lt;rules&gt; &lt;logger name="*" minlevel="Trace" writeTo="smartLog"/&gt; &lt;/rules&gt; &lt;/nlog&gt; &lt;/configuration&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload