- Notifications
You must be signed in to change notification settings - Fork1.4k
WebService target Workaround for url variables
Rolf Kristensen edited this pageMay 31, 2025 ·8 revisions
⚠️ NLog 5.0 now supports Layout for theUrl
-parameter, so this work-around is no longer needed.
*a target configured as in:https://github.com/NLog/NLog/wiki/Webservice-Target-for-Splunk
- Create a transformation from nlog.config using the slow-cheetah plugin:https://github.com/microsoft/slow-cheetah/blob/master/doc/transforming_files.md
- Create a Nlog.Release.config (XML-File) to override the hard coded url
- The name must be same as the name from your original target!
- Install Package "Microsoft.VisualStudio.SlowCheetah"
- Update your csproj file:
<ItemGroup> <ContentUpdate="Nlog.config"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <TransformOnBuild>true</TransformOnBuild> </Content> <NoneInclude="Nlog.Release.config"> <IsTransformFile>true</IsTransformFile> <DependentUpon>Nlog.config</DependentUpon> </None> </ItemGroup>
<?xml version="1.0" encoding="utf-8"?><!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134.--><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targetsasync="true"> <targetxsi:type="WebService"name="Splunk"url="NEWURL"protocol="JsonPost"encoding="utf-8"preAuthenticate="true"xdt:Transform="SetAttributes(url)"xdt:Locator="Match(name)"> </target> </targets></nlog>
- In program.cs modify nlog.config before configuring Nlog
var token = Environment.GetEnvironmentVariable("YOURURL"); if (!string.IsNullOrWhiteSpace(token)) { var currentFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var nlogFolder = $"{currentFolder}/nlog.config"; var configText = File.ReadAllText(nlogFolder); var updatedConfig = configText .Replace("NEWURL", token); File.WriteAllText(nlogFolder, updatedConfig); }var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();//...
-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json