But it works on my PC!

The random thoughts of Richard Fennell on technology and software development

Lost in nLog configuration files

I have been trying to track down a performance problem today on an ASP.NET MVC. It all turned out to be down to an incorrect connection string in a nLog.config file for logging to a SQL DB. As soon as I commented out the Db target for nLog my login to the web site was virtually instant as opposed to taking 30 seconds (what i assume is a SQL timeout)

I had suspected a problem with the logged as I was not seeing anything in DebugView, but it all took a while to track down as i did not seem to get any logging output.

It seems the blocker was if I had Visual Studio 2012 running in debug mode then any output to the OutputDebugString was lost, this took forever to realise.  The only truly reliable target was that of a text file. I had expected the logging messages to appear in the VS debug window – it did not.

So this is what worked as a user who is a local administrator, but with UAC enable and NOT running any of the tools as administrator (so nothing special).

  1. Load DebugView  with default settings.
  2. From in VS2012 start the web site without debugging (so it loads IIS Express and IE)
  3. Load the web page with some logging
  4. The logging appears in the file, debugview and the DB

This was done using the nlog.config file

<?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"   >

  <targets>
    <target xsi:type="Database" name="db"
       commandText="INSERT INTO [LogEntries](TimeStamp, Message, Level, Logger) VALUES(getutcdate(), @msg, @level, @logger)"
       connectionString="server=.\sql2012;database=MyDb;integrated security=sspi" dbProvider="System.Data.SqlClient" >
      <parameter name="@msg" layout="${message}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />
    </target>

    <target name="ds" xsi:type="OutputDebugString" layout=" ${message}"/>

    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />

  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="db" />
    <logger name="*" minlevel="Trace" writeTo="ds" />
    <logger name="*" minlevel="Trace" writeTo="f" />
  </rules>
</nlog>

Comments are closed