Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, 07 August 2013
( NLog | this.Log )

Log to the output window in VS

<!--<target name="debugger" xsi:type="Debugger" layout="${logger}::${message}"/>—>

<!-- See output window in VS-->
<!--<logger name="*" minlevel="Debug" maxlevel="Warn" writeTo="debugger" />—>

DB

http://stackoverflow.com/questions/1008093/how-to-get-nlog-to-write-to-database

<target type="Database" name="database" connectionStringName="DBConnect">
  <commandText>
    insert into Log ([CreateDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace);
  </commandText>
  <parameter name="@createDate" layout="${date}"/>
  <parameter name="@origin" layout="${logger}"/>
  <parameter name="@logLevel" layout="${level}"/>
  <parameter name="@message" layout="${message}"/>
  <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/>
  <parameter name="@stackTrace" layout="${stacktrace}"/>
  <parameter name="@stackTrace" layout=""/>
</target>

then


<logger name="*" appendTo="database"/>

NLog Errors

Very useful for debugging why something is not logging.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true" internalLogFile="c:\temp\nlog.txt" internalLogLevel="Debug">

| | # 
# Wednesday, 10 July 2013
( NLog | this.Log )

http://devlicio.us/blogs/rob_reynolds/archive/2012/12/15/introducing-this-log.aspx

  • It does no logging if you don’t have a logging engine set up.
  • It works everywhere in your code base (where you can write C#). This means in your razor views as well!
  • It uses deferred execution, which means you don’t have to mock it to use it with testing (your tests won’t fail on logging lines).
  • You can mock it easily and use that as a means of testing.
  • You have no references to your actual logging engine anywhere in your codebase, so swapping it out (or upgrading) becomes a localized event to one class where you provide the adapter.

I’ve been using NLog as the engine.

 

Use NuGet to pull down this.log and NLog

public class SomeClass { 
  
  public void SomeMethod() { 
    this.Log().Info(() => "Here is a log message with params which can be in Razor Views as well: '{0}'".FormatWith(typeof(SomeClass).Name)); 
 
    this.Log().Debug("I don't have to be delayed execution or have parameters either"); 
  } 
 
  public static void StaticMethod() { 
    "SomeClass".Log().Error("This is crazy, right?!"); 
  } 
  
}

and to setup:

LoggingExtensions.Logging.Log.InitializeWith<LoggingExtensions.NLog.NLogLog>();

and in App.Config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="file" type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
        <!--<target type="File" fileName="${basedir}/logs/UER.Web.log" layout="${longdate} | ${pad:padding=-4:inner=${threadid}}| ${pad:padding=-10:inner=[${level:uppercase=true}]}| ${pad:padding=-50:inner=${logger}}| ${message}" archiveFileName="${basedir}/logs/UER.Web-archive-{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="14" concurrentWrites="true" keepFileOpen="false" encoding="iso-8859-2" />-->
        <target type="File" fileName="e:/temp/UER.Web.log" layout="${longdate} | ${pad:padding=-4:inner=${threadid}}| ${pad:padding=-10:inner=[${level:uppercase=true}]}| ${pad:padding=-50:inner=${logger}}| ${message}" archiveFileName="${basedir}/logs/UER.Web-archive-{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="14" concurrentWrites="true" keepFileOpen="false" encoding="iso-8859-2" />
      </target>
      <target name="file-exceptions" type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
        <target type="File" fileName="${basedir}/logs/UER.Web-exceptions.log" layout="${longdate} | ${machinename} | ${processinfo:property=ProcessName} | ${threadid}|[${level:uppercase=true}] | (${logger})${newline}[START]${newline}${message}${newline}[END]" archiveFileName="${basedir}/logs/UER.Web-exceptions-archive-{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="14" concurrentWrites="true" keepFileOpen="false" encoding="iso-8859-2" />
      </target>

      <!--<target name="console" type="ColoredConsole" layout="${longdate} [${level:uppercase=true}] ${message}">
        <highlight-row backgroundColor="NoChange" condition="level = LogLevel.Error" foregroundColor="Red" />
        <highlight-row backgroundColor="NoChange" condition="level = LogLevel.Warn" foregroundColor="Yellow" />
      </target>-->
      
      
      <!--<target name="errorSmtp" type="Mail" layout="${longdate} ${threadid} [${level:uppercase=true}] ${logger} - ${message}" subject="this.Log Sample Console Errors - LOCAL DEBUG" to="noone@noreply.org" from="this.Log.sample@noreply.org" smtpServer="localhost" />-->
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" maxlevel="Warn" writeTo="file" />
      <!--<logger name="*" minlevel="Debug" maxlevel="Warn" writeTo="console" />-->
      <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="file-exceptions" />
      <!--<logger name="*" minlevel="Error" writeTo="errorSmtp" />-->
    </rules>
  </nlog>

When using Resharper’s test runner, I used e:/temp to dump log files to, with permissions set to everyone writeable.

| | #