{"id":9274,"date":"2022-03-15T20:23:16","date_gmt":"2022-03-15T20:23:16","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9274"},"modified":"2022-03-15T20:23:16","modified_gmt":"2022-03-15T20:23:16","slug":"nlog-embedding-runtime-variables","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/nlog-embedding-runtime-variables\/","title":{"rendered":"NLog embedding runtime variables"},"content":{"rendered":"<p>Sometimes you&#8217;ll want to set your NLog configuration up to output the log files to an environment, i.e. %APPDATA%\/MyApp\/Logging\/UAT &#8211; but you&#8217;ll only know the selected environment at runtime.<\/p>\n<p>This is where we can embed ${var:NAME}, where NAME is a key to your variable. In our case this will be ENV for environment.<\/p>\n<p>So for example, let&#8217;s assume we have this fileName in your NLog configuration like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfileName=&quot;${specialfolder:folder=ApplicationData}\/MyApp\/Logging\/${var:ENV}\/logfile_{$shortdate}.log\r\n<\/pre>\n<p>As you can see from the above, we&#8217;ve included the built-in <em>specialfolder<\/em> and <em>shortdate<\/em> variables. For the ${var:ENV} we need to supply using &#8220;env&#8221; variable in our code, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLogManager.Configurations.Variables&#x5B;&quot;env&quot;] = GetCurrentEnvironment()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you&#8217;ll want to set your NLog configuration up to output the log files to an environment, i.e. %APPDATA%\/MyApp\/Logging\/UAT &#8211; but you&#8217;ll only know the selected environment at runtime. This is where we can embed ${var:NAME}, where NAME is a key to your variable. In our case this will be ENV for environment. So for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[326],"tags":[],"class_list":["post-9274","post","type-post","status-publish","format-standard","hentry","category-nlog"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9274"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9274\/revisions"}],"predecessor-version":[{"id":9279,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9274\/revisions\/9279"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}