Fixed filename in Nlog incorporating date and time - filenames

I want to be able to set the target's filename to be a fixed name that includes the process's start time and date even if the configuration file is updated due to autoReload="true" being in the nlog intro.
I have tried the following but it recreates the log file when the log file is altered.
<target
name="file"
xsi:type="File"
fileName="C:\Logs\${cached:cached=true:inner=${date:format=yyyyMMdd.HHmmss}}.${processid}.txt"
layout="${time} ${level}: ${message}">
</target>
Is there a way that I can get a time and date that is essentially fixed for the duration of the process no matter how many times the config file is updated and incorporate it into the filename?

You could pass a custom parameter to NLog, containing the process start date and time.
GlobalDiagnosticsContext.Set("ProcessStart", yourDateVariable);
logger.Debug("Hello world!");
And then use that as part of the filename.
${gdc:ProcessStart}

Related

Can MSBuild ItemGroup's be chunked?

I've got an ItemGroup that includes source files from my project:
<ItemGroup>
<SourceFiles Include=".\**\*.h;.\**\*.cpp"/>
</ItemGroup>
There are a few hundred source files. I want to pass them to a command line tool in an Exec task.
If I call the command line tool individually for each file:
<Exec Command="tool.exe %(SourceFiles.FullPath)" WorkingDirectory="."/>
Then, it runs very slowly.
If I call the command line tool and pass all of the files in one go:
<Exec Command="tool.exe #(SourceFiles -> '"%(FullPath)"', ' ')" WorkingDirectory="."/>
Then, I get an error if there are too many files (I'm guessing the command line length exceeds some maximum).
Is there a way I can chunk the items so that the tool can be called a number of times, each time passing up to a maximum number of source file names to the tool?
I'm not aware of any mechanism to do that with well known item metadata. What you could do is load all those paths into their own item group and write a custom task that calls the exec task. Writing a custom task is pretty simple, it can be done inline:
http://msdn.microsoft.com/en-us/library/vstudio/dd722601(v=vs.100).aspx

DB Script generation using fluentmigrator

I'm using fluentmigrator and I'm stuck with a problem I need to create DB script using fluentmigrator each time I run the build script and its done but the problem is I just want to rewrite the script only if the db is altered . How can I achieve that my current code is given below
<Target Name="Migrate" >
<MakeDir Directories="$(OutputFolder)\DBScripts"></MakeDir>
<Migrate Database="sqlserver2008"
Connection="Data Source=ALen-PC;Initial Catalog=TestMigrator;User ID=user;Password=password"
Target="$(OutputFolder)\Release\bin\MigratorTest.dll"
Output="True"
OutputFilename="$(OutputFolder)\DBScripts\DBScript.sql">
</Migrate>
</Target>
At the moment, there is no support for this process in FluentMigrator. You could add a timestamp to the filename and then check the size of the file. If it is very small, less than 200 bytes then throw it away. If it is larger than 200 bytes then the schema has changed so rename the file to DBScript.sql and replace the previous version.
I would recommend submitting this as a feature request for FluentMigrator here.

The output parameter 'CopiedFiles' of Copy task is returning all the files specified to copy even if it copies nothing given SkipUnchangedFiles="true"

The CopiedFiles parameter is returning all the files that were intended to be copied. But given the fact that SkipUnchangedFiles is set to true and ttask itself is not copying anything as can be seen on command line (no copying message). Why not, then, CopiedFiles is empty?
I need to have CopiedFiles parameter be populated only with files that were actually copied (because they were changed) in order to further copy these files into some other folder. This is to maintain an up-to-date release folder as well as to extract only those files which actually need to be propogated onto UAT/production server.
For reference sake, the copy task code I'm using is given below:
<Copy SkipUnchangedFiles="true"
SourceFiles="#(cfile)"
DestinationFiles="#(cfile->'$(PublishDir)\%(Identity)')">
<Output
TaskParameter="CopiedFiles"
ItemName="Changed" />
</Copy>
<Message Text="changed:#(Changed)" Importance="high" />
Is there a bug in the copy task or this is the intended behavior.
The behavior you are seeing is by design. MSBuild keeps track of file dependencies using task outputs. If it were to do otherwise, anything that relied on the #(Changed) item array as an input would not fully process all of the files it needed in most cases. It will even keep track of properties and items created within targets that don't even execute when Inputs and Outputs are up-to-date, for the same reason. Consider making a different Copy task of your own with an additional output parameter, CopiedFilesCopiedByTask (this naming mirrors the naming and behavior of the ValueSetByTask in the otherwise defunct CreateProperty task).

Is it possible to define customised datestamp in MQFTE using Ant Script?

I need to create a custom stamp for the day of the week, which is to be appended to the filename.
Suppose If it is sunday , the value of the stamp must be "1",
same way if its monday the value of the stamp must be "2".
Is this possible in MQFTE using Ant Script???
You can't do this directly with standard TStamp task. That allows you to format your timestamp using patterns defined in SimpleDateFormat, and there is no format symbol for day of week as a number.
I guess you could write a custom TStamp task.
However, this works.
Create a set of properties files, named using 3-letter day name:
$ find daysOfWeek/
daysOfWeek/
daysOfWeek/Fri.properties
daysOfWeek/Mon.properties
daysOfWeek/Sat.properties
daysOfWeek/Sun.properties
daysOfWeek/Thu.properties
daysOfWeek/Tue.properties
daysOfWeek/Wed.properties
In each file, define a single property for the corresponding day number, e.g.
$ cat daysOfWeek/Thu.properties
day.num=5
In your build script, first get a property matching today's day name, then load the corresponding properties file, then you can reference the day.num property.
<project>
<tstamp>
<format property="day.name" pattern="E" locale="en/US"/>
</tstamp>
<property file="daysOfWeek/${day.name}.properties"/>
<echo message="${day.name}"/>
<echo message="${day.num}"/>
</project>
Output today (Thursday) is
$ ant
Buildfile: build.xml
[echo] Thu
[echo] 5
BUILD SUCCESSFUL
Total time: 0 seconds

MSBUILD Executing only Changed SQL Scripts

I need to construct an MSBUILD script executes .SQL Scripts which have changed since last build.
I initially thought that I could copy all scripts from one folder to another using the <Copy> task and using the CopiedFiles <Output> for the copy task. However the copy task returns All files that it Attempted to copy, not actual copied files.
I am able to get MSBUILD to execute SQL Scripts via MSBUILD.ExtensionPack but Im scratching my head on this one
You can do this with a concept known as incremental building. The idea is that you would create a target and then specify the inputs and outputs, which would be files. MSBuild will compare the timestamps of the input files to the output files. If all outputs were created after all outputs then the target is skipped. If all inputs are newer then all the target will be executed for all files. If only a portion are out of date, then only those will be passed to the target. For more info on this see the section Using Incremental Builds in my article Best Practices For Creating Reliable Builds, Part 2.
Also for more resources on MSBuild I have compiled a list at http://sedotech.com/Resources#MSBuild
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="RunScripts">
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
<PropertyGroup>
<ConnStr>Server=Example;Database=Example;Trusted_Connection=True</ConnStr>
<BuildFolder>Build\</BuildFolder>
</PropertyGroup>
<ItemGroup>
<Scripts Include="*.sql"/>
</ItemGroup>
<Target Name="RunScripts"
Inputs="#(Scripts)"
Outputs="#(Scripts->'$(BuildFolder)%(Filename)%(Extension)')">
<SqlExecute TaskAction="ExecuteScalar"
Files="#(Scripts)"
ConnectionString="$(ConnStr)"/>
<Copy SourceFiles="#(Scripts)"
DestinationFiles="#(Scripts->'$(BuildFolder)%(Filename)%(Extension)')"/>
</Target>
</Project>
Could it be that you copying into an empty destination?
SkipUnchangedFiles
If true, skips the copying of files that are unchanged
between the source and destination. The Copy task considers
files to be unchanged if they have the same size and the
same last modified time.
In your case i suspect that all files are considered changed since they don't exist at the destination.