-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WebService target Workaround for url variables
Rolf Kristensen edited this page Nov 27, 2021
·
7 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>
<Content Update="Nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<TransformOnBuild>true</TransformOnBuild>
</Content>
<None Include="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. -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Information" internalLogFile="c:\temp\internal-nlog.txt" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<targets async="true">
<target xsi: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
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json