Using web.config transforms with NLog configuration

I’m building a web based hybrid system that can run on-premises and on cloud. On development machine I want NLog to write logs on disk. When system is deployed to cloud I want NLog to write logs to Windows Azure Table storage. In this posting I will show you how to use web.config transforms with NLog configuration.

For NLog running under web application the first place to look for configuration is web.config file. Starting from Visual Studio 2010 web.config files have support for transforms so you have root web.config and then child configuration files that inherit all settings in root. Those child configuration files may define transforms that modify settings inherited from root file. We are using this mechanism in this hybrid system.

NLog configuration

As with other custom configurations we need configuration section definition in web.config for NLog:

<section name=nlog type=NLog.Config.ConfigSectionHandler, NLog/>

After configuration sections definition we add configuration block for NLog:

<nlog

    autoReload=true

    throwExceptions=true>

  <variable name=appName value=YourAppName />

 

  <extensions>

    <add assembly=Minirent.SaaS.Azure />

  </extensions>

  <targets async=true>

    <target type=File

            name=default

            layout=${longdate} – ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}

            fileName=${basedir}\logs\\Debug.${shortdate}.log

            keepFileOpen=false

            archiveFileName=${basedir}\logs\\Debug_${shortdate}.{##}.log

            archiveNumbering=Sequence

            archiveEvery=Day

            maxArchiveFiles=30

          />

  </targets>

  <rules>

    <logger name=* writeTo=default minlevel=Info />

  </rules>

</nlog>

NB! Don’t use xmlns and other namespace declarations for NLog. If you use then during transformation something gets messed up and all attributes with no namespace prefix get automatically generated prefix. It may be okay for transformation engine but it is not for NLog that is not able to understand XML like this.

By default, as you can see, I’m using file logger. It works also on cloud but these logs will be lost when Windows Azure replaces virtual machine where system is deployed.

Adding configuration for cloud

For Windows Azure we are using NLog target that writes log entries to Windows Azure Table Storage. This way we can use multiple instances of virtual machines and we can be sure that logs are not deleted when virtual machine is replaced.

For Windows Azure we use release config. This is child configuration file called. Web.Release.config. In this file we replace the whole NLog configuration block with Windows Azure one.

<nlog

    autoReload=true

    throwExceptions=true xdt:Transform=Replace>

 

  <variable name=appName value=YourAppName />

 

  <extensions>

    <add assembly=Minirent.SaaS.Azure />

  </extensions>

  <targets async=true>

    <target type=AzureStorage name=azure tableStorageConnectionStringName=StorageConnectionString />

  </targets>

  <rules>

    <logger name=* writeTo=azure minlevel=Info />

  </rules>

</nlog>

You can see here Replace-transform used (xdt:Transform attribute of root node). When we make deployment to Windows Azure then release configuration is used and original NLog block in web.config is replaced by one shown here.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    3 thoughts on “Using web.config transforms with NLog configuration

    • August 14, 2015 at 3:00 pm
      Permalink

      Thanks!.. I missed the ” part… :-)

    • September 21, 2016 at 10:36 pm
      Permalink

      I really don’t want to put nlog configuration inside web.config. It is much better when it is in a separate nlog.config, so the logging configuration can be tweaked without forcing the app to be reloaded.

    • Pingback:Software development bookmarks | Ordo Rerum

    Leave a Reply

    Your email address will not be published. Required fields are marked *