DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Using Event-Driven Ansible to Monitor Your Web Application
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Azure Deployment Using FileZilla

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Navigating Double and Triple Extortion Tactics
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • Comparing Managed Postgres Options on The Azure Marketplace

Using web.config Transforms with NLog Configuration

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Oct. 24, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
10.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Related Posts

  • Visual Studio add-in: CopySourceAsHTML
  • ASP.NET MVC 4: Display modes
  • ASP.NET Web API: How content negotiation works?
  • Deploying ASP.NET MVC 3 web application to server where ASP.NET MVC 3 is not installed
  • ASP.NET Web API: Query string based content formatting

The post Using web.config transforms with NLog configuration appeared first on Gunnar Peipman - Programming Blog.

Web application ASP.NET MVC

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Using Event-Driven Ansible to Monitor Your Web Application
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Azure Deployment Using FileZilla

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!