NLog not using UTF-8 encoding - encoding

Since I upgraded NLog to the latest version 5.1.0, logging special characters (e.g. Ö, ä or ß) stopped working. Outout to file is like: ö ä ü for these chars.
I checked the log file with NotePad++ and it says that the encoding is ANSI. However, I have configured NLog to use utf-8. My config:
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/nLog.csv" archiveAboveSize="50000000" archiveNumbering="Sequence" maxArchiveFiles="3" encoding="utf-8" keepFileOpen="true">
<layout xsi:type="CsvLayout">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level}"/>
<column name="category" layout="${event-context:item=category}" />
<column name="message" layout="${message}" />
</layout>
</target>
What am I missing?

Based on the comments, I was able to solve the problem. However, I do not know what the real cause of the problem was.
Solution: remove encoding="utf-8" from the nLog config AND read the log file as Encoding.UTF-8 (i.e. when opening the file with a StreamReader).
I don't know why this works since nLog uses UTF-8 as default...

Related

Controlling the sequence of events in a Wixtoolset (.msi) installer

I am creating a Microsoft Installer (.msi file) using the Wixtoolset (Windows Installer XML). This installer must automate the installation of an existing .exe program (named installer.exe below) and copy a custom configuration file (named settings.conf below) to the target directory. In addition the installer must modify the configuration file using the InstallFiles command below. But the timing of events is critical. If the executable installer runs too early, it fails or exhibits strange behavior. And if the executable installer run too late in the install sequence, it overwrites my modified configuration file with the generic values. I believe this can be done by assigning a string to the Before or After property value. What Before or After property assignment will allow the executable to run properly but not overwrite the configuration file I moved by the CopyFile element? Here is my Wixtoolset XML code.
<Property Id="CONFIGFOLDER" Value="C:\acme\config" >
<Feature
Id="ConfigurationFile"
Title="Configuration File"
Level="1"
<ComponentRef Id="CMP_ACME_Config_File" />
</Feature>
<DirectoryRef Id="TARGETDIR">
<Component Id="CMP_ACME_Config_File" Guid="">
<File
Id="ACME_Config"
Source="MySettings.conf"
KeyPath="yes"
<CopyFile Id="Copy_ACME_Config"
DestinationProperty="CONFIGFOLDER"
DestinationName="settings.conf" />
</File>
</Component>
</DirectoryRef>
<Binary
Id="InstallerEXE"
SourceFile="installer.exe" />
<CustomAction
Id="Launch_Installer"
BinaryKey="InstallerEXE"
Impersonate="yes"
Execute="deferred"
ExeCommand=""
Return="check" />
<InstallExecuteSequence>
<Custom Action="Launch_Installer"
Before="InstallFiles">
</Custom>
</InstallExecuteSequence>
</Property>
I can't explain exactly why this works but assigning "InstallFiles" to the "After" property in the "Custom" element seems to do the trick.
<InstallExecuteSequence>
<Custom Action="Launch_Installer"
After="InstallFiles">
</Custom>
</InstallExecuteSequence>

Mule flow adding weird characters to beginning of file

I'm simply moving files, but for some reason they are appending weird characters to the front of the file.
Before:
(Encoded with UTF-8)
testCol1,testCol2,testCol3
After: (Encoded with ANSI)
¬í ur [B¬óøTà xp žÌtestCol1,testCol2,testCol3
Things I've tried:
Explicitly setting the encoding to utf-8.
Explicitly setting the encoding to ANSI.
Using a Object to Byte Array with encoding set to utf-8.
Using a Object to String with encoding set to utf-8.
Here is my flow and relevant connectors:
<sftp:connector name="Sftp" maxConnectionPoolSize="3" doc:name="SFTP"/>
<file:connector name="FileConnector" autoDelete="true" streaming="false" validateConnections="true" doc:name="FileConnector"/>
<flow name="SftpOutBound" initialState="stopped">
<file:inbound-endpoint
path="${ftp.base}/${id}/export"
pollingFrequency="${polling.frequency.millis}"
responseTimeout="${standard.response.timeout.millis}"
fileAge="${standard.fileage.delay.millis}"
connector-ref="deletingFileConnector"
doc:name="OutBound">
<file:filename-wildcard-filter pattern="${out.filter}" />
</file:inbound-endpoint>
<object-to-byte-array-transformer doc:name="Object to Byte Array" />
<all doc:name="all">
<file:outbound-endpoint
path="${archive.out}/${id}"
outputPattern="#[header:INBOUND:originalFileName]_#[function:datestamp:${standard.date.format}]_#[function:systime]"
responseTimeout="${standard.response.timeout.millis}"
connector-ref="deletingFileConnector"
doc:name="Archive" />
<logger
level="WARN"
message="Uploaded file from ${ftp.base}/${id}/export/#[header:INBOUND:originalFileName]"
doc:name="Logger"/>
<sftp:outbound-endpoint
connector-ref="Sftp"
host="${host}"
port="${port}"
path="${in.path}"
user="${user}"
identityFile="${configFullPath}/${identity.file}"
passphrase="${passphrase}"
responseTimeout="${standard.response.timeout.millis}"
keepFileOnError="true"
duplicateHandling="overwrite"
exchange-pattern="one-way"
outputPattern="#[header:INBOUND:originalFileName]"
doc:name="SFTP"/>
</all>
</flow>
Thanks for any help!
So it seems to be an issue with Mule 3.5.0, when I upgraded to 3.6.1 the issue went away. Still not sure what was causing it, but the version change solved it.
The payload result from file:inbound-endpoint is of type InputStream. Why don't you leave as is (byte[]) to be saved by the file and sftp outbound end-points?
If you have to convert it into a string, then I suggest using <byte-array-to-string-transformer /> after you read it instead of <object-to-byte-array-transformer />

WiX Bootstrapper: How do I set burn variables from the command line?

Using WiX 3.7 and .NET 4.0.
How does one set burn variables when running a WiX bootstrapper EXE from the command line?
First of all, the burn variables that you wish to set need to be set as Overridable. To do this you must include the follow namespace in your WXS: xmlns:bal="http://schemas.microsoft.com/wix/BalExtension" and if you're using Visual Studio like me you need to include WixBalExtension.dll in your project references. Next you need to add the following attribute to all of the burn variables that you want to set via the command line: bal:Overridable="yes".
Now you can set the variables via the command line in this fashion:
BootstrapperSetup.exe /i /passive MyBurnVariable1=1 MyBurnVariable2=2
Below is an example of a WXS file that satifies all of the conditions described above:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:bal="http://schemas.microsoft.com/wix/BalExtension">
<Bundle Name="MyProduct" Version="1.0.0" Manufacturer="MyManufacturer" UpgradeCode="PUT-UPGRADE-CODE-HERE">
<BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.HyperlinkLicense">
<bal:WixStandardBootstrapperApplication LicenseUrl="MyLicense.htm" ThemeFile="MyThemeFile.xml" LocalizationFile="MyLocFile.wxl" />
</BootstrapperApplicationRef>
<Variable Name="MyBurnVariable1" bal:Overridable="yes" Type="numeric" Value="0" />
<Variable Name="MyBurnVariable2" bal:Overridable="yes" Type="numeric" Value="0" />
<Chain>
<MsiPackage Id="MyFirstMsiPackage"
SourceFile="first.msi"
InstallCondition="MyBurnVariable1 = 1" />
<MsiPackage Id="MySecondMsiPackage"
SourceFile="second.msi">
<MsiProperty Name="MY_PROPERTY" Value="[MyBurnVariable2]" />
</MsiPackage>
</Chain>
</Bundle>
</Wix>

How do I optionally require a command line arguement for Ant?

I'm new to ant, and I want to require a file name if something other than the default target is used, so the calling syntax would be something like this:
ant specifictarget -Dfile=myfile
I'm using the ant contrib package to give me additional functionality, so I have this:
<if>
<equals arg1="${file}" arg2="" />
<then>
<!-- fail here -->
</then>
</if>
My thinking is that if file was not specified it might be equal to the empty string. Obviously, this didn't work, and I'm not finding any examples on google or the right syntax in the manual.
So what syntax should I use?
You don't really need the contrib package. This is more conveniently done using built-in ant capabilities like if/unless and depends. See below:
<target name="check" unless="file" description="check that the file property is set" >
<fail message="set file property in the command line (e.g. -Dfile=someval)"/>
</target>
<target name="specifictarget" if="file" depends="check" description=" " >
<echo message="do something ${file}"/>
</target>
You've got the right idea. The
ant specifictarget -Dfile=myfile
sets Ant Properties from the command line. All you really need is
<property name="file" value=""/>
for your default value. That way if file is not specified, it will be equal to the empty string.
Since properties are not mutable in Ant, you can add this:
<property name="file" value="" />
This will set the property file to an empty string if it hasn't already been set on the command line. Then your equality test will work as you intended.
Alternately, you can use escape the value since ant just spits out the actual text when it can't do a property substitution.
<if>
<equals arg1="${file}" arg2="$${file}" />
<then>
<echo>BARF!</echo>
</then>
</if>

MSBuild ReadLinesFromFile all text on one line

When I do a ReadLinesFromFile on a file in MSBUILD and go to output that file again, I get all the text on one line. All the Carriage returns and LineFeeds are stripped out.
<Project DefaultTargets = "Deploy"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<ItemGroup>
<MyTextFile Include="$(ReleaseNotesDir)$(NewBuildNumber).txt"/>
</ItemGroup>
<Target Name="ReadReleaseNotes">
<ReadLinesFromFile
File="#(MyTextFile)" >
<Output
TaskParameter="Lines"
ItemName="ReleaseNoteItems"/>
</ReadLinesFromFile>
</Target>
<Target Name="MailUsers" DependsOnTargets="ReadReleaseNotes" >
<Mail SmtpServer="$(MailServer)"
To="$(MyEMail)"
From="$(MyEMail)"
Subject="Test Mail Task"
Body="#(ReleaseNoteItems)" />
</Target>
<Target Name="Deploy">
<CallTarget Targets="MailUsers" />
</Target>
</Project>
I get the text from the file which normally looks like this
- New Deployment Tool for BLAH
- Random other stuff()""
Coming out like this
- New Deployment Tool for BLAH;- Random other stuff()""
I know that the code for ReadLinesFromFile will pull the data in one line at a time and strip out the carriage returns.
Is there a way to put them back in?
So my e-mail looks all nicely formatted?
Thanks
The problem here is you are using the ReadLinesFromFile task in a manner it wasn't intended.
ReadLinesFromFile Task
Reads a list of items from a text file.
So it's not just reading all the text from a file, it's reading individual items from a file and returning an item group of ITaskItems. Whenever you output a list of items using the #() syntax you will get a separated list, the default of which is a semicolon. This example illustrates this behavior:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<Color Include="Red" />
<Color Include="Blue" />
<Color Include="Green" />
</ItemGroup>
<Target Name="Build">
<Message Text="ItemGroup Color: #(Color)" />
</Target>
</Project>
And the output looks like this:
ItemGroup Color: Red;Blue;Green
So while the best solution to your problem is to write an MSBuild task that reads a file into a property as a string an not a list of items, that's really not what you asked for. You asked if there was a way to put them back, and there is using MSBuild Transforms.
Transforms are used to create one list from another and also have the ability to transform using a custom separator. So the answer is to transform your list read in using ReadItemsFromFile into another list with newlines. Here is an example that does just that:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<File Include="$(MSBuildProjectDirectory)\Test.txt" />
</ItemGroup>
<Target Name="Build">
<ReadLinesFromFile File="#(File)">
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<Message Text="FileContents: #(FileContents)" />
<Message Text="FileContents Transformed: #(FileContents->'%(Identity)', '%0a%0d')" />
</Target>
</Project>
Test.text looks like:
Red
Green
Blue
And the output looks like this:
[C:\temp]:: msbuild test.proj
Microsoft (R) Build Engine Version 3.5.21022.8
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 11/8/2008 8:16:59 AM.
Project "C:\temp\test.proj" on node 0 (default targets).
FileContents: Red;Green;Blue
FileContents Transformed: Red
Green
Blue
Done Building Project "C:\temp\test.proj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.03
What's going on here is two things.
#(FileContents->'%(Identity)', '%0a%0d')
We are transforming the list from one type to another using the same values (Identity) but a custom separator '%0a%0d'
We are using MSBuild Escaping to escape the line feed (%0a) and carriage return (%0d)
If you are using MSBuild 4.0, you can do the following instead, to get the contents of a file:
$([System.IO.File]::ReadAllText($FilePath))
Instead of #(FileContents->'%(Identity)', '%0a%0d') I believe you can do #(FileContents, '%0a%0d')
You can use WriteLinesToFile combined with
$([System.IO.File]::ReadAllText($(SourceFilePath))):
< WriteLinesToFile File="$(DestinationFilePath)" Lines="$([System.IO.File]::ReadAllText($(SourceFilePath)))"
Overwrite="true"
/>
This will copy your file exactly at it is.