How to set label prefix application version with CruiseControl.NET - cruisecontrol.net

We're using Cruise Control v1.8.5. We have next scenario:
checkout source code from git repository
run some scripts to build project (e.g. npm install && cordova clean, cordova build, also minify some css files, compile typescript ...)
Now we want to add label on successfull build. Therefore I found that cruisecontrol has labeller option, so I added:
<cb:define name="Mylabeller">
<labeller type="defaultlabeller">
<initialBuildLabel>1</initialBuildLabel>
<labelFormat>0</labelFormat>
<incrementOnFailure>true</incrementOnFailure>
<labelPrefixFile>X:\BuildFiles\myProject\version.txt</labelPrefixFile>
<labelPrefixFileSearchPattern>\d\.\d\.\d\.</labelPrefixFileSearchPattern>
</labeller>
</cb:define>
But the problem is that, this is done before source code is retrieved (git) from repository. I read version from my project and then CruiseControl with regex reads version and outputs something like: 1.0.3.buildNumber.
How to set/update label prefix version after retrieving source code from repository. It reads old one, before checkout. I also read about dynamic variables, can they be somehow set or something?
Appending project's xml:
<cb:scope ProjDirName="myProject">
<project name="myProject" queue="Daily" queuePriority="30">
<workingDirectory>D:\BuildFiles\$(ProjDirName)\Working</workingDirectory>
<artifactDirectory>D:\BuildFiles\$(ProjDirName)\Artifacts</artifactDirectory>
<cb:sourcecontrol module="Modul1" TagOnSuccess="false" Branch="$(BranchDevelop)" />
<tasks>
<cb:NpmInstall />
<cb:Build />
</tasks>
<cb:Mylabeller />
<cb:commonall />
</project>
</cb:scope>

Figured that CruiseControl sets next build version when project build is started and before project's xml is read (checkout, tasks, ...).
CruiseControl project build log:
<parameters>
<parameter name="$CCNetArtifactDirectory" value="D:\BuildFiles\MyProject\Artifacts" />
<parameter name="$CCNetBuildCondition" value="ForceBuild" />
<parameter name="$CCNetBuildDate" value="2016-01-25" />
<parameter name="$CCNetBuildId" value="9d8234720e7342a3aa636aacc120eb13" />
<parameter name="$CCNetBuildTime" value="13:49:42" />
<parameter name="$CCNetFailureTasks" value="System.Collections.ArrayList" />
<parameter name="$CCNetFailureUsers" value="System.Collections.ArrayList" />
<parameter name="$CCNetIntegrationStatus" value="Unknown" />
<parameter name="$CCNetLabel" value="1.0.88" />
<parameter name="$CCNetLastIntegrationStatus" value="Failure" />
<parameter name="$CCNetListenerFile" value="D:\BuildFiles\MyProject\Artifacts\MyProject_ListenFile.xml" />
<parameter name="$CCNetModifyingUsers" value="System.Collections.ArrayList" />
<parameter name="$CCNetNumericLabel" value="88" />
<parameter name="$CCNetProject" value="MyProject" />
<parameter name="$CCNetProjectUrl" value="http://BUILDer/ccnet" />
<parameter name="$CCNetRequestSource" value="machineName1" />
<parameter name="$CCNetUser" value="user1" />
<parameter name="$CCNetWorkingDirectory" value="D:\BuildFiles\MyProject\Working" />
<parameter name="$LastChangeNumber" value="96ece86d55f83c8eb129cbfeb01724a3d37bb18a" />
Also I makes sense that Continuous Integration dictates application version e.g. major.minor.build instead that each commit dictates version.
Solution:
<cb:scope ProjDirName="myProject">
<project name="myProject" queue="Daily" queuePriority="30">
<workingDirectory>D:\BuildFiles\$(ProjDirName)\Working</workingDirectory>
<artifactDirectory>D:\BuildFiles\$(ProjDirName)\Artifacts</artifactDirectory>
<cb:sourceControlMyProject Branch="develop"/>
<cb:dailytrigger />
<tasks>
<cb:NpmInstall />
<cb:BuildRelease />
<cb:PublishAfter />
</tasks>
<publishers>
<cb:commonpublish/>
</publishers>
<cb:MyLabeller LabelPrefix="1.0." /> <!-- version is fixed -->
<cb:commonall />
</project>
</cb:scope>
And labeller:
<cb:define name="MyLabeller">
<labeller type="defaultlabeller">
<initialBuildLabel>1</initialBuildLabel>
<prefix>$(LabelPrefix)</prefix>
<incrementOnFailure>true</incrementOnFailure>
<labelFormat>0</labelFormat>
</labeller>
</cb:define>
Currently this setup is ok. In case we'll be developing version 1.1 upper setup must be changed. Main point was to get versioning working e.g. 1.0. + cruiseControl_next_build_number.

Related

NLog Database Error Could not load file or assembly 'Microsoft.Data.SqlClient, Culture=neutral, PublicKeyToken=null'

Trying to get NLog logging to the database. Running IIS with .net 7. Have the following packages included in my project
Image of Project Packages
Getting the following error:
Error DatabaseTarget(Name=MessageStoredProcedure): Failed to create ConnectionType from DBProvider=Microsoft.Data.SqlClient.SqlConnection,Microsoft.Data.SqlClient Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Data.SqlClient, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Microsoft.Data.SqlClient, Culture=neutral, PublicKeyToken=null'
My nlog looks like this
<target name="asyncReqResMessage" type="AsyncWrapper">
<target type="Database" name="MessageStoredProcedure">
<keepConnection>true</keepConnection>
<dbProvider>Microsoft.Data.SqlClient.SqlConnection,Microsoft.Data.SqlClient</dbProvider>
<connectionString>Data Source=****.company.org,1433; Initial Catalog=Logging; User ID=*****; Password=****; Connection Timeout=30; Application Name=test;</connectionString>
<commandText>EXEC logInsertExternalRequest #EndPoint, #Method, #LogKey, #Server, #TokenId, #ReceivedDate, #RequestType, #RequestIP, #RequestHeaders, #RequestMessage, #ResponseDate, #ResponseHeaders, #ResponseMessage, #ResponseCode, #ResponseId, #Duration</commandText>
<parameter name="#EndPoint" layout="${event-properties:logInfo:objectpath=EndPoint}" />
<parameter name="#Method" layout="${event-properties:logInfo:objectpath=Method}" />
<parameter name="#LogKey" layout="${event-properties:logInfo:objectpath=LogKey}" />
<parameter name="#Server" layout="${machinename}" />
<parameter name="#TokenId" layout="${event-properties:logInfo:objectpath=TokenId}" />
<parameter name="#ReceivedDate" layout="${event-properties:logInfo:objectpath=ReceivedDate}" />
<parameter name="#RequestIP" layout="${event-properties:logInfo:objectpath=RequestIP}" />
<parameter name="#RequestType" layout="${event-properties:logInfo:objectpath=RequestType}" />
<parameter name="#RequestHeaders" layout="${event-properties:logInfo:objectpath=RequestHeaders}" />
<parameter name="#RequestMessage" layout="${event-properties:logInfo:objectpath=RequestMessage}" />
<parameter name="#ResponseDate" layout="${event-properties:logInfo:objectpath=ResponseDate}" />
<parameter name="#ResponseHeaders" layout="${event-properties:logInfo:objectpath=ResponseHeaders}" />
<parameter name="#ResponseMessage" layout="${event-properties:logInfo:objectpath=ResponseMessage}" />
<parameter name="#ResponseCode" layout="${event-properties:logInfo:objectpath=ResponseCode}" />
<parameter name="#ResponseId" layout="${event-properties:logInfo:objectpath=ResponseId}" />
<parameter name="#Duration" layout="${event-properties:logInfo:objectpath=Duration}" />
</target>
</target>
Made sure the Microsoft.Data.SqlClient.dll is in the bin directory.
Made sure I updated the dbprovder to the following: Microsoft.Data.SqlClient.SqlConnection,Microsoft.Data.SqlClient

NLog DB Configuration

From the docs https://github.com/NLog/NLog/wiki/Database-target
It seems settings are shown as attributes on the target element such as:
<target xsi:type="Database"
name="String"
dbUserName="Layout"
dbProvider="String"
and in the example below as separate child nodes:
<target name="database" xsi:type="Database">
<connectionStringName>NLogDb</connectionStringName>
Neither work for me, I just get Invalid configuration exceptions with this message:
NotSupportedException: Parameter connectionStringName not supported on DatabaseTarget
The Config File:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
throwExceptions="true"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Database"
name="database"
keepConnection="true"
useTransactions="true"
dbProvider="System.Data.SqlClient"
connectionStringName="DefaultConnection"
commandText="INSERT INTO Logs (EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (#EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage)">
<parameter name="#EventDateTime" layout="${date:s}" />
<parameter name="#EventLevel" layout="${level}" />
<parameter name="#UserName" layout="${aspnet-user-identity}" />
<parameter name="#MachineName" layout="${machinename}" />
<parameter name="#EventMessage" layout="${message}" />
<parameter name="#ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="#ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="#ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="#ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="#InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile,database" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxLevel="Info" final="true
" />
<!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
How it is being called in program.cs
var logger = NLog.Web.NLogBuilder.ConfigureNLog( "nlog.config" ).GetCurrentClassLogger();
(copied from their docs)
Must be missing something obvious, but since there is conflicting info in the docs, and copying other people's configs posted on here, not sure where to go with it
Looks like you are running on NetCore. NLog is not able to read connectionStringName from AppSettings.json as you have found out yourself (Requires extra dependencies to access IConfiguration).
One possible solution is using this extension:
https://www.nuget.org/packages/NLog.Appsettings.Standard/
And use connectionString (Instead of connectionStringName) in NLog.config:
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Appsettings.Standard"/>
</extensions>
<target xsi:type="Database" connectionString="${appsettings:name=ConnectionStrings.DefaultConnection}">
Alternative solution is to assign a GDC variable before logging:
NLog.GlobalDiagnosticsContext.Set("DefaultConnection", Configuration.GetConnectionString("DefaultConnection"));
And then use GDC in NLog.config:
<target xsi:type="Database" connectionString="${gdc:item=DefaultConnection}">
See also https://github.com/NLog/NLog/wiki/Gdc-layout-renderer
Update NLog.Extension.Logging ver. 1.4.0
With NLog.Extension.Logging ver. 1.4.0 then you can now use ${configsetting}
See also: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

How do I make aspnet-request available in my NLog config?

I am trying to add the ip address to logging. I installed the NLog.Extended package and ensured that the NLog.Extended.dll is present in my bin next to the base NLog.dll. I have added the variable "${aspnet-request:serverVariable=remote_addr}" to my layout renderer. I get a generic error saying:
An error occurred creating the configuration section handler for nlog: Exception occurred when loading configuration from [my Web.config]
Here is my NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Extended" />
</extensions>
<targets async="true">
<target name="fileLog" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${aspnet-request:serverVariable=remote_addr} ${longdate} ${callsite} ${level} ${message} ${exception:format=ToString}" />
<target name="dbLog" xsi:type="Database" connectionStringName="db.data" commandText="insert into log ([Date], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (#date, #origin, #logLevel, #message, #exception, #stackTrace)">
<parameter name="#date" layout="${date}"/>
<parameter name="#origin" layout="${callsite}"/>
<parameter name="#logLevel" layout="${level}"/>
<parameter name="#message" layout="${message}"/>
<parameter name="#exception" layout="${exception:format=Message,StackTrace}"/>
<parameter name="#stackTrace" layout="${stacktrace}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="dbLog" />
<logger name="*" minlevel="Trace" writeTo="fileLog" />
</rules>
</nlog>
When I remove the AspNetRequest variable it doesn't complain. I've tried replacing "remote_addr" with "remote_host" with no change. This project is using NLog 2.1.0. Any ideas on what I'm doing wrong here?
If you're using NLog 4.0 then you'll need NLog.Web to use the aspnet=* layout renderers.
It turned out that the NLog.Extended.dll(3.1.0.0) had to match the NLog.dll it was referencing. I upgraded the NLog.dll to 3.1.0.0 and everything is working now.
In my ASP.NET webapi 2.0 solution I had a separate projects for my API's and logging. Using nLog 4.4.12 installed through NuGet and all the helper packages (Config,Extended,Web,Schema) in the logging project.
The "aspnet-request" properties in my config would not work until I added the nLog references to my api project, even though "Copy Local" is set to true for all the nLog DLL's in the logging project. My nLog section from my web.config is below.
Hope this help someone,
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="db" xsi:type="Database" connectionStringName="NLogConn" commandType="StoredProcedure" commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="#machineName" layout="${machinename}" />
<parameter name="#siteName" layout="${iis-site-name}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#username" layout="${aspnet-user-identity}" />
<parameter name="#message" layout="${message}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#properties" layout="${all-event-properties:separator=|}" />
<parameter name="#serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="#port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="#url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="#https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="#serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="#remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="#callSite" layout="${callsite:fileName=true:includeSourcePath=false:skipFrames=1}" />
<parameter name="#exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="db" />
</rules>

Conditional Include on Items in an Item Group

I want to copy files over to a server but before i do this i would like to include the latest msi file that i generate.
I noticed that the ItemGroup and Item have a Condition attribute but i do not know how to utilize this to include the latest file.
So far this is my setup:
<Target Name="AfterBuild">
<ItemGroup>
<Installers Include="\\SERVERNAME\BuildOutput\ProductStream\**\Installers\Customer\Installer.msi"/>
</ItemGroup>
<Message Text="FirstItem: %(Installers.Filename)" />
<Message Text="FirstItem: %(Installers.FullPath)" />
The output of this are two files:
e.g
\\Servername\BuildOutput\ProductStream\Installers\ProductStreamV2.1.1202.1402\Installer.msi
\\Servername\BuildOutput\ProductStream\Installers\ProductStreamV2.1.1405.1301\Installer.msi
I want to include the 2.1.1405.1301 build in the Item as this is the latest one.
I would appreciate if someone would assist me because i cannot find how to go about doing this from the MSDN blogs.
Thanks,
You could use a custom task for this purpose. It allows you to filter items any way you want. Here I used regular expressions to select the latest installer:
<Target Name="AfterBuild">
<ItemGroup>
<Installers Include="**\Installer.msi"/>
</ItemGroup>
<SelectLatestInstaller Installers="#(Installers)">
<Output TaskParameter="LatestInstaller" ItemName="LatestInstaller" />
</SelectLatestInstaller>
<Message Text="%(LatestInstaller.FullPath)" />
</Target>
<UsingTask TaskName="SelectLatestInstaller"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Installers ParameterType="System.String[]" Required="true" />
<LatestInstaller ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Linq" />
<Using Namespace="System" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
LatestInstaller = Installers
.OrderByDescending(f => Regex.Match(f, #"\\ProductStreamV(?<version>(\d+.){0,3}\d+)\\").Groups["version"].Value)
.FirstOrDefault();
]]>
</Code>
</Task>
</UsingTask>

How to force nlog to throw an exception when the logging to database fails?

When I take down the database that backs nlog, nothing gets get logged and it seems NLog swallows the problem. Is there any way to configure it to raise and exception or at least to log in a text file that logging failed?
Here is what my configuration looks like:
<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug"
internalLogToConsole="true">
<targets>
<!--Useful for debugging-->
<target name="consolelog" type="ColoredConsole"
layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
<target name="databaselog" type="Database">
<dbProvider>System.Data.SqlClient</dbProvider>
<!-- database connection parameters -->
<!-- alternatively you could provide a single 'connectionstring' parameter -->
<connectionString>Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI</connectionString>
<commandText>
insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(#time_stamp,#level,#host,#type,#source,#logger,#message,#stacktrace,#allxml);
</commandText>
<parameter name="#time_stamp" layout="${utc_date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#host" layout="${machinename}" />
<parameter name="#type" layout="${exception:format=type}" />
<parameter name="#source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#message" layout="${message}" />
<parameter name="#stacktrace" layout="${exception:stacktrace}" />
<parameter name="#allxml" layout="${web_variables}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="databaselog" />
</rules>
</nlog>
You can force Nlog to throw exception when sql server is not reached by following
<nlog throwExceptions="true">
... your nlog config
</nlog>
More info here,
http://nlog-project.org/2010/09/05/new-exception-handling-rules-in-nlog-2-0.html
It's a new feature in v2.0 so you need v2.0.
It will not work in earlier versions.
Also checkout following configuration info
https://github.com/NLog/NLog/wiki/Logging-Troubleshooting
which allows Nlog to log it's own exceptions to a specified file.
Does NLog.config have the property "Copy to Output Directory" set as "Copy always"?
I think you have wrong NLog.config file: you use elements instead of attributes within the target (documentation). Should be something like this:
<target
name="databaselog"
type="Database"
dbProvider="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI"
commandText="insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(#time_stamp,#level,#host,#type,#source,#logger,#message,#stacktrace,#allxml);">
<parameter name="#time_stamp" layout="${utc_date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#host" layout="${machinename}" />
<parameter name="#type" layout="${exception:format=type}" />
<parameter name="#source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#message" layout="${message}" />
<parameter name="#stacktrace" layout="${exception:stacktrace}" />
<parameter name="#allxml" layout="${web_variables}" />
</target>

Resources